1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.Assert.assertArrayEquals;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.UnsupportedEncodingException;
29  import java.lang.reflect.Constructor;
30  import java.lang.reflect.Method;
31  import java.lang.reflect.Modifier;
32  import java.nio.CharBuffer;
33  import java.nio.charset.Charset;
34  import java.util.Arrays;
35  import java.util.Collections;
36  import java.util.Iterator;
37  import java.util.Locale;
38  
39  import org.apache.commons.lang3.text.WordUtils;
40  import org.junit.Test;
41  
42  /**
43   * Unit tests {@link org.apache.commons.lang3.StringUtils}.
44   *
45   * @version $Id$
46   */
47  @SuppressWarnings("deprecation") // deliberate use of deprecated code
48  public class StringUtilsTest {
49      
50      static final String WHITESPACE;
51      static final String NON_WHITESPACE;
52      static final String HARD_SPACE;
53      static final String TRIMMABLE;
54      static final String NON_TRIMMABLE;
55      static {
56          String ws = "";
57          String nws = "";
58          final String hs = String.valueOf(((char) 160));
59          String tr = "";
60          String ntr = "";
61          for (int i = 0; i < Character.MAX_VALUE; i++) {
62              if (Character.isWhitespace((char) i)) {
63                  ws += String.valueOf((char) i);
64                  if (i > 32) {
65                      ntr += String.valueOf((char) i);
66                  }
67              } else if (i < 40) {
68                  nws += String.valueOf((char) i);
69              }
70          }
71          for (int i = 0; i <= 32; i++) {
72              tr += String.valueOf((char) i);
73          }
74          WHITESPACE = ws;
75          NON_WHITESPACE = nws;
76          HARD_SPACE = hs;
77          TRIMMABLE = tr;
78          NON_TRIMMABLE = ntr;
79      }
80  
81      private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
82      private static final String[] EMPTY_ARRAY_LIST = {};
83      private static final String[] NULL_ARRAY_LIST = {null};
84      private static final Object[] NULL_TO_STRING_LIST = {
85          new Object(){
86              @Override
87              public String toString() {
88                  return null;
89              }
90          }
91      };
92      private static final String[] MIXED_ARRAY_LIST = {null, "", "foo"};
93      private static final Object[] MIXED_TYPE_LIST = {"foo", Long.valueOf(2L)};
94      private static final long[] LONG_PRIM_LIST = {1, 2};
95      private static final int[] INT_PRIM_LIST = {1, 2};
96      private static final byte[] BYTE_PRIM_LIST = {1, 2};
97      private static final short[] SHORT_PRIM_LIST = {1, 2};
98      private static final char[] CHAR_PRIM_LIST = {'1', '2'};
99      private static final float[] FLOAT_PRIM_LIST = {1, 2};
100     private static final double[] DOUBLE_PRIM_LIST = {1, 2};
101 
102     private static final String SEPARATOR = ",";
103     private static final char   SEPARATOR_CHAR = ';';
104 
105     private static final String TEXT_LIST = "foo,bar,baz";
106     private static final String TEXT_LIST_CHAR = "foo;bar;baz";
107     private static final String TEXT_LIST_NOSEP = "foobarbaz";
108 
109     private static final String FOO_UNCAP = "foo";
110     private static final String FOO_CAP = "Foo";
111 
112     private static final String SENTENCE_UNCAP = "foo bar baz";
113     private static final String SENTENCE_CAP = "Foo Bar Baz";
114 
115     //-----------------------------------------------------------------------
116     @Test
117     public void testConstructor() {
118         assertNotNull(new StringUtils());
119         final Constructor<?>[] cons = StringUtils.class.getDeclaredConstructors();
120         assertEquals(1, cons.length);
121         assertTrue(Modifier.isPublic(cons[0].getModifiers()));
122         assertTrue(Modifier.isPublic(StringUtils.class.getModifiers()));
123         assertFalse(Modifier.isFinal(StringUtils.class.getModifiers()));
124     }
125     
126     //-----------------------------------------------------------------------
127     @Test
128     public void testIsEmpty(){
129       assertTrue(StringUtils.isEmpty(null));
130       assertTrue(StringUtils.isEmpty(""));
131       assertFalse(StringUtils.isEmpty(" "));
132       assertFalse(StringUtils.isEmpty("bob"));
133       assertFalse(StringUtils.isEmpty("  bob  "));
134     }
135     
136     @Test
137     public void testIsNotEmpty(){
138       assertFalse(StringUtils.isNotEmpty(null));
139       assertFalse(StringUtils.isNotEmpty(""));
140       assertTrue(StringUtils.isNotEmpty(" "));
141       assertTrue(StringUtils.isNotEmpty("bob"));
142       assertTrue(StringUtils.isNotEmpty("  bob  "));
143     }
144     
145     @Test
146     public void testIsAnyEmpty(){
147       assertTrue(StringUtils.isAnyEmpty((String) null));
148       assertTrue(StringUtils.isAnyEmpty((String[]) null));
149       assertTrue(StringUtils.isAnyEmpty(null, "foo"));
150       assertTrue(StringUtils.isAnyEmpty("", "bar"));
151       assertTrue(StringUtils.isAnyEmpty("bob", ""));
152       assertTrue(StringUtils.isAnyEmpty("  bob  ", null));
153       assertFalse(StringUtils.isAnyEmpty(" ","bar"));
154       assertFalse(StringUtils.isAnyEmpty("foo","bar"));
155     }
156     
157     @Test
158     public void testIsNoneEmpty(){
159       assertFalse(StringUtils.isNoneEmpty((String) null));
160       assertFalse(StringUtils.isNoneEmpty((String[]) null));
161       assertFalse(StringUtils.isNoneEmpty(null, "foo"));
162       assertFalse(StringUtils.isNoneEmpty("", "bar"));
163       assertFalse(StringUtils.isNoneEmpty("bob", ""));
164       assertFalse(StringUtils.isNoneEmpty("  bob  ", null));
165       assertTrue(StringUtils.isNoneEmpty(" ", "bar"));
166       assertTrue(StringUtils.isNoneEmpty("foo", "bar"));
167     }
168     
169     @Test
170     public void testIsBlank(){
171       assertTrue(StringUtils.isBlank(null));
172       assertTrue(StringUtils.isBlank(""));
173       assertTrue(StringUtils.isBlank(" "));
174       assertFalse(StringUtils.isBlank("bob"));
175       assertFalse(StringUtils.isBlank("  bob  "));
176     }
177     
178     @Test
179     public void testIsNotBlank(){
180       assertFalse(StringUtils.isNotBlank(null));
181       assertFalse(StringUtils.isNotBlank(""));
182       assertFalse(StringUtils.isNotBlank(" "));
183       assertTrue(StringUtils.isNotBlank("bob"));
184       assertTrue(StringUtils.isNotBlank("  bob  "));
185     }
186     
187      @Test
188     public void testIsAnyBlank(){
189       assertTrue(StringUtils.isAnyBlank((String) null));
190       assertTrue(StringUtils.isAnyBlank((String[]) null));
191       assertTrue(StringUtils.isAnyBlank(null, "foo"));
192       assertTrue(StringUtils.isAnyBlank(null, null));
193       assertTrue(StringUtils.isAnyBlank("", "bar"));
194       assertTrue(StringUtils.isAnyBlank("bob", ""));
195       assertTrue(StringUtils.isAnyBlank("  bob  ", null));
196       assertTrue(StringUtils.isAnyBlank(" ","bar"));
197       assertFalse(StringUtils.isAnyBlank("foo","bar"));
198     }
199     
200     @Test
201     public void testIsNoneBlank(){
202       assertFalse(StringUtils.isNoneBlank((String) null));
203       assertFalse(StringUtils.isNoneBlank((String[]) null));
204       assertFalse(StringUtils.isNoneBlank(null, "foo"));
205       assertFalse(StringUtils.isNoneBlank(null, null));
206       assertFalse(StringUtils.isNoneBlank("", "bar"));
207       assertFalse(StringUtils.isNoneBlank("bob", ""));
208       assertFalse(StringUtils.isNoneBlank("  bob  ", null));
209       assertFalse(StringUtils.isNoneBlank(" ", "bar"));
210       assertTrue(StringUtils.isNoneBlank("foo", "bar"));
211     }
212     
213     //-----------------------------------------------------------------------
214     @Test
215     public void testCaseFunctions() {
216         assertNull(StringUtils.upperCase(null));
217         assertNull(StringUtils.upperCase(null, Locale.ENGLISH));
218         assertNull(StringUtils.lowerCase(null));
219         assertNull(StringUtils.lowerCase(null, Locale.ENGLISH));
220         assertNull(StringUtils.capitalize(null));
221         assertNull(StringUtils.uncapitalize(null));
222 
223         assertEquals("capitalize(empty-string) failed",
224                 "", StringUtils.capitalize("") );
225         assertEquals("capitalize(single-char-string) failed",
226                 "X", StringUtils.capitalize("x") );
227         assertEquals("capitalize(String) failed",
228                 FOO_CAP, StringUtils.capitalize(FOO_CAP) );
229         assertEquals("capitalize(string) failed",
230                 FOO_CAP, StringUtils.capitalize(FOO_UNCAP) );
231 
232         assertEquals("uncapitalize(String) failed",
233                 FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) );
234         assertEquals("uncapitalize(string) failed",
235                 FOO_UNCAP, StringUtils.uncapitalize(FOO_UNCAP) );
236         assertEquals("uncapitalize(empty-string) failed",
237                 "", StringUtils.uncapitalize("") );
238         assertEquals("uncapitalize(single-char-string) failed",
239                 "x", StringUtils.uncapitalize("X") );
240                      
241         // reflection type of tests: Sentences.
242         assertEquals("uncapitalize(capitalize(String)) failed",
243                      SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(SENTENCE_UNCAP)) );
244         assertEquals("capitalize(uncapitalize(String)) failed",
245                      SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)) );
246 
247         // reflection type of tests: One word.
248         assertEquals("uncapitalize(capitalize(String)) failed",
249                      FOO_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(FOO_UNCAP)) );
250         assertEquals("capitalize(uncapitalize(String)) failed",
251                      FOO_CAP, StringUtils.capitalize(StringUtils.uncapitalize(FOO_CAP)) );
252 
253         assertEquals("upperCase(String) failed",
254                      "FOO TEST THING", StringUtils.upperCase("fOo test THING") );
255         assertEquals("upperCase(empty-string) failed",
256                      "", StringUtils.upperCase("") );
257         assertEquals("lowerCase(String) failed",
258                      "foo test thing", StringUtils.lowerCase("fOo test THING") );
259         assertEquals("lowerCase(empty-string) failed",
260                      "", StringUtils.lowerCase("") );
261 
262         assertEquals("upperCase(String, Locale) failed",
263                      "FOO TEST THING", StringUtils.upperCase("fOo test THING", Locale.ENGLISH) );
264         assertEquals("upperCase(empty-string, Locale) failed",
265                      "", StringUtils.upperCase("", Locale.ENGLISH) );
266         assertEquals("lowerCase(String, Locale) failed",
267                      "foo test thing", StringUtils.lowerCase("fOo test THING", Locale.ENGLISH) );
268         assertEquals("lowerCase(empty-string, Locale) failed",
269                      "", StringUtils.lowerCase("", Locale.ENGLISH) );
270     }
271 
272     @Test
273     public void testSwapCase_String() {
274         assertNull(StringUtils.swapCase(null));
275         assertEquals("", StringUtils.swapCase(""));
276         assertEquals("  ", StringUtils.swapCase("  "));
277         
278         assertEquals("i", WordUtils.swapCase("I") );
279         assertEquals("I", WordUtils.swapCase("i") );
280         assertEquals("I AM HERE 123", StringUtils.swapCase("i am here 123") );
281         assertEquals("i aM hERE 123", StringUtils.swapCase("I Am Here 123") );
282         assertEquals("I AM here 123", StringUtils.swapCase("i am HERE 123") );
283         assertEquals("i am here 123", StringUtils.swapCase("I AM HERE 123") );
284         
285         final String test = "This String contains a TitleCase character: \u01C8";
286         final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
287         assertEquals(expect, WordUtils.swapCase(test));
288     }
289 
290     //-----------------------------------------------------------------------
291     @Test
292     public void testJoin_Objects() {
293         assertEquals("abc", StringUtils.join("a", "b", "c"));
294         assertEquals("a",   StringUtils.join(null, "", "a"));
295         assertNull( StringUtils.join((Object[])null));
296     }
297 
298     @Test
299     public void testJoin_Objectarray() {
300 //        assertNull(StringUtils.join(null)); // generates warning
301         assertNull(StringUtils.join((Object[]) null)); // equivalent explicit cast
302         // test additional varargs calls
303         assertEquals("", StringUtils.join(new Object[0])); // empty array
304         assertEquals("", StringUtils.join((Object) null)); // => new Object[]{null}
305 
306         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
307         assertEquals("", StringUtils.join(NULL_ARRAY_LIST));
308         assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST));
309         assertEquals("abc", StringUtils.join(new String[] {"a", "b", "c"}));
310         assertEquals("a", StringUtils.join(new String[] {null, "a", ""}));
311         assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST));
312         assertEquals("foo2", StringUtils.join(MIXED_TYPE_LIST));
313     }
314         
315     @Test
316     public void testJoin_ArrayCharSeparator() {
317         assertNull(StringUtils.join((Object[]) null, ','));
318         assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR));
319         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
320         assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR_CHAR));
321         assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR_CHAR));
322 
323         assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0, MIXED_ARRAY_LIST.length-1));
324         assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1));
325         assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST,'/', 0, 1));
326         assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 2));
327         assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, '/', 1, 2));
328         assertEquals("", StringUtils.join(MIXED_TYPE_LIST, '/', 2, 1));
329     }
330     
331     @Test
332     public void testJoin_ArrayOfChars() {
333         assertNull(StringUtils.join((char[]) null, ','));
334         assertEquals("1;2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR));
335         assertEquals("2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
336     }
337     
338     @Test
339     public void testJoin_ArrayOfBytes() {
340         assertNull(StringUtils.join((byte[]) null, ','));
341         assertEquals("1;2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR));
342         assertEquals("2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
343     }
344     
345     @Test
346     public void testJoin_ArrayOfInts() {
347         assertNull(StringUtils.join((int[]) null, ','));
348         assertEquals("1;2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR));
349         assertEquals("2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
350     }
351     
352     @Test
353     public void testJoin_ArrayOfLongs() {
354         assertNull(StringUtils.join((long[]) null, ','));
355         assertEquals("1;2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR));
356         assertEquals("2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
357     }
358     
359     @Test
360     public void testJoin_ArrayOfFloats() {
361         assertNull(StringUtils.join((float[]) null, ','));
362         assertEquals("1.0;2.0", StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR));
363         assertEquals("2.0", StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
364     }
365     
366     @Test
367     public void testJoin_ArrayOfDoubles() {
368         assertNull(StringUtils.join((double[]) null, ','));
369         assertEquals("1.0;2.0", StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR));
370         assertEquals("2.0", StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
371     }
372     
373     @Test
374     public void testJoin_ArrayOfShorts() {
375         assertNull(StringUtils.join((short[]) null, ','));
376         assertEquals("1;2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR));
377         assertEquals("2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR, 1, 2));
378     }
379     
380     @Test
381     public void testJoin_ArrayString() {
382         assertNull(StringUtils.join((Object[]) null, null));
383         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, null));
384         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, ""));
385         
386         assertEquals("", StringUtils.join(NULL_ARRAY_LIST, null));
387         
388         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, null));
389         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, ""));
390         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR));
391 
392         assertEquals(TEXT_LIST, StringUtils.join(ARRAY_LIST, SEPARATOR));
393         assertEquals(",,foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR));
394         assertEquals("foo,2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR));
395 
396         assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, "/", 0, MIXED_ARRAY_LIST.length-1));
397         assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, "", 0, MIXED_ARRAY_LIST.length-1));
398         assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, "/", 0, 1));
399         assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, "/", 0, 2));
400         assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, "/", 1, 2));
401         assertEquals("", StringUtils.join(MIXED_TYPE_LIST, "/", 2, 1));
402     }
403     
404     @Test
405     public void testJoin_IteratorChar() {
406         assertNull(StringUtils.join((Iterator<?>) null, ','));
407         assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR_CHAR));
408         assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
409         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
410         assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), 'x'));
411     }
412     
413     @Test
414     public void testJoin_IteratorString() {
415         assertNull(StringUtils.join((Iterator<?>) null, null));
416         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), null));
417         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), ""));
418         assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), "x"));
419         assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), null));
420 
421         assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), null));
422         
423         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), null));
424         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), ""));
425         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR));
426         
427         assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR));
428 
429         assertNull(StringUtils.join(Arrays.asList(NULL_TO_STRING_LIST).iterator(), SEPARATOR));
430     }
431 
432     @Test
433     public void testJoin_IterableChar() {
434         assertNull(StringUtils.join((Iterable<?>) null, ','));
435         assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR_CHAR));
436         assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST), SEPARATOR_CHAR));
437         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), SEPARATOR_CHAR));
438         assertEquals("foo", StringUtils.join(Collections.singleton("foo"), 'x'));
439     }
440 
441     @Test
442     public void testJoin_IterableString() {
443         assertNull(StringUtils.join((Iterable<?>) null, null));
444         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST), null));
445         assertEquals(TEXT_LIST_NOSEP, StringUtils.join(Arrays.asList(ARRAY_LIST), ""));
446         assertEquals("foo", StringUtils.join(Collections.singleton("foo"), "x"));
447         assertEquals("foo", StringUtils.join(Collections.singleton("foo"), null));
448 
449         assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST), null));
450 
451         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), null));
452         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), ""));
453         assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST), SEPARATOR));
454 
455         assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR));
456     }
457 
458     @Test
459     public void testSplit_String() {
460         assertNull(StringUtils.split(null));
461         assertEquals(0, StringUtils.split("").length);
462         
463         String str = "a b  .c";
464         String[] res = StringUtils.split(str);
465         assertEquals(3, res.length);
466         assertEquals("a", res[0]);
467         assertEquals("b", res[1]);
468         assertEquals(".c", res[2]);
469         
470         str = " a ";
471         res = StringUtils.split(str);
472         assertEquals(1, res.length);
473         assertEquals("a", res[0]);
474         
475         str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
476         res = StringUtils.split(str);
477         assertEquals(2, res.length);
478         assertEquals("a", res[0]);
479         assertEquals("b" + NON_WHITESPACE + "c", res[1]);                       
480     }
481     
482     @Test
483     public void testSplit_StringChar() {
484         assertNull(StringUtils.split(null, '.'));
485         assertEquals(0, StringUtils.split("", '.').length);
486 
487         String str = "a.b.. c";
488         String[] res = StringUtils.split(str, '.');
489         assertEquals(3, res.length);
490         assertEquals("a", res[0]);
491         assertEquals("b", res[1]);
492         assertEquals(" c", res[2]);
493             
494         str = ".a.";
495         res = StringUtils.split(str, '.');
496         assertEquals(1, res.length);
497         assertEquals("a", res[0]);
498         
499         str = "a b c";
500         res = StringUtils.split(str,' ');
501         assertEquals(3, res.length);
502         assertEquals("a", res[0]);
503         assertEquals("b", res[1]);
504         assertEquals("c", res[2]);
505     }
506     
507     @Test
508     public void testSplit_StringString_StringStringInt() {
509         assertNull(StringUtils.split(null, "."));
510         assertNull(StringUtils.split(null, ".", 3));
511         
512         assertEquals(0, StringUtils.split("", ".").length);
513         assertEquals(0, StringUtils.split("", ".", 3).length);
514         
515         innerTestSplit('.', ".", ' ');
516         innerTestSplit('.', ".", ',');
517         innerTestSplit('.', ".,", 'x');
518         for (int i = 0; i < WHITESPACE.length(); i++) {
519             for (int j = 0; j < NON_WHITESPACE.length(); j++) {
520                 innerTestSplit(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j));
521                 innerTestSplit(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j));
522             }
523         }
524         
525         String[] results;
526         final String[] expectedResults = {"ab", "de fg"};
527         results = StringUtils.split("ab   de fg", null, 2);
528         assertEquals(expectedResults.length, results.length);
529         for (int i = 0; i < expectedResults.length; i++) {
530             assertEquals(expectedResults[i], results[i]);
531         }
532         
533         final String[] expectedResults2 = {"ab", "cd:ef"};
534         results = StringUtils.split("ab:cd:ef",":", 2);
535         assertEquals(expectedResults2.length, results.length);
536         for (int i = 0; i < expectedResults2.length; i++) {
537             assertEquals(expectedResults2[i], results[i]);
538         }
539     }
540     
541     private void innerTestSplit(final char separator, final String sepStr, final char noMatch) {
542         final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
543             "), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
544         
545         final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
546         String[] res;
547         // (str, sepStr)
548         res = StringUtils.split(str, sepStr);
549         assertEquals(msg, 3, res.length);
550         assertEquals(msg, "a", res[0]);
551         assertEquals(msg, "b", res[1]);
552         assertEquals(msg, noMatch + "c", res[2]);
553         
554         final String str2 = separator + "a" + separator;
555         res = StringUtils.split(str2, sepStr);
556         assertEquals(msg, 1, res.length);
557         assertEquals(msg, "a", res[0]);
558 
559         res = StringUtils.split(str, sepStr, -1);
560         assertEquals(msg, 3, res.length);
561         assertEquals(msg, "a", res[0]);
562         assertEquals(msg, "b", res[1]);
563         assertEquals(msg, noMatch + "c", res[2]);
564         
565         res = StringUtils.split(str, sepStr, 0);
566         assertEquals(msg, 3, res.length);
567         assertEquals(msg, "a", res[0]);
568         assertEquals(msg, "b", res[1]);
569         assertEquals(msg, noMatch + "c", res[2]);
570         
571         res = StringUtils.split(str, sepStr, 1);
572         assertEquals(msg, 1, res.length);
573         assertEquals(msg, str, res[0]);
574         
575         res = StringUtils.split(str, sepStr, 2);
576         assertEquals(msg, 2, res.length);
577         assertEquals(msg, "a", res[0]);
578         assertEquals(msg, str.substring(2), res[1]);
579     }
580 
581     @Test
582     public void testSplitByWholeString_StringStringBoolean() {
583         assertArrayEquals( null, StringUtils.splitByWholeSeparator( null, "." ) ) ;
584 
585         assertEquals( 0, StringUtils.splitByWholeSeparator( "", "." ).length ) ;
586 
587         final String stringToSplitOnNulls = "ab   de fg" ;
588         final String[] splitOnNullExpectedResults = { "ab", "de", "fg" } ;
589 
590         final String[] splitOnNullResults = StringUtils.splitByWholeSeparator( stringToSplitOnNulls, null ) ;
591         assertEquals( splitOnNullExpectedResults.length, splitOnNullResults.length ) ;
592         for ( int i = 0 ; i < splitOnNullExpectedResults.length ; i+= 1 ) {
593             assertEquals( splitOnNullExpectedResults[i], splitOnNullResults[i] ) ;
594         }
595 
596         final String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiously" ;
597 
598         final String[] splitOnStringExpectedResults = { "abstemiously", "abstemiously" } ;
599         final String[] splitOnStringResults = StringUtils.splitByWholeSeparator( stringToSplitOnCharactersAndString, "aeiouy" ) ;
600         assertEquals( splitOnStringExpectedResults.length, splitOnStringResults.length ) ;
601         for ( int i = 0 ; i < splitOnStringExpectedResults.length ; i+= 1 ) {
602             assertEquals( splitOnStringExpectedResults[i], splitOnStringResults[i] ) ;
603         }
604 
605         final String[] splitWithMultipleSeparatorExpectedResults = {"ab", "cd", "ef"};
606         final String[] splitWithMultipleSeparator = StringUtils.splitByWholeSeparator("ab:cd::ef", ":");
607         assertEquals( splitWithMultipleSeparatorExpectedResults.length, splitWithMultipleSeparator.length );
608         for( int i = 0; i < splitWithMultipleSeparatorExpectedResults.length ; i++ ) {
609             assertEquals( splitWithMultipleSeparatorExpectedResults[i], splitWithMultipleSeparator[i] ) ;
610         }
611     }
612 
613     @Test
614     public void testSplitByWholeString_StringStringBooleanInt() {
615         assertArrayEquals( null, StringUtils.splitByWholeSeparator( null, ".", 3 ) ) ;
616 
617         assertEquals( 0, StringUtils.splitByWholeSeparator( "", ".", 3 ).length ) ;
618 
619         final String stringToSplitOnNulls = "ab   de fg" ;
620         final String[] splitOnNullExpectedResults = { "ab", "de fg" } ;
621         //String[] splitOnNullExpectedResults = { "ab", "de" } ;
622 
623         final String[] splitOnNullResults = StringUtils.splitByWholeSeparator( stringToSplitOnNulls, null, 2 ) ;
624         assertEquals( splitOnNullExpectedResults.length, splitOnNullResults.length ) ;
625         for ( int i = 0 ; i < splitOnNullExpectedResults.length ; i+= 1 ) {
626             assertEquals( splitOnNullExpectedResults[i], splitOnNullResults[i] ) ;
627         }
628 
629         final String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiouslyaeiouyabstemiously" ;
630 
631         final String[] splitOnStringExpectedResults = { "abstemiously", "abstemiouslyaeiouyabstemiously" } ;
632         //String[] splitOnStringExpectedResults = { "abstemiously", "abstemiously" } ;
633         final String[] splitOnStringResults = StringUtils.splitByWholeSeparator( stringToSplitOnCharactersAndString, "aeiouy", 2 ) ;
634         assertEquals( splitOnStringExpectedResults.length, splitOnStringResults.length ) ;
635         for ( int i = 0 ; i < splitOnStringExpectedResults.length ; i++ ) {
636             assertEquals( splitOnStringExpectedResults[i], splitOnStringResults[i] ) ;
637         }
638     }
639 
640     @Test
641     public void testSplitByWholeSeparatorPreserveAllTokens_StringStringInt() {
642         assertArrayEquals( null, StringUtils.splitByWholeSeparatorPreserveAllTokens( null, ".", -1 ) ) ;
643 
644         assertEquals( 0, StringUtils.splitByWholeSeparatorPreserveAllTokens( "", ".", -1 ).length ) ;
645 
646         // test whitespace
647         String input = "ab   de fg" ;
648         String[] expected = new String[] { "ab", "", "", "de", "fg" } ;
649 
650         String[] actual = StringUtils.splitByWholeSeparatorPreserveAllTokens( input, null, -1 ) ;
651         assertEquals( expected.length, actual.length ) ;
652         for ( int i = 0 ; i < actual.length ; i+= 1 ) {
653             assertEquals( expected[i], actual[i] );
654         }
655 
656         // test delimiter singlechar
657         input = "1::2:::3::::4";
658         expected = new String[] { "1", "", "2", "", "", "3", "", "", "", "4" };
659 
660         actual = StringUtils.splitByWholeSeparatorPreserveAllTokens( input, ":", -1 ) ;
661         assertEquals( expected.length, actual.length ) ;
662         for ( int i = 0 ; i < actual.length ; i+= 1 ) {
663             assertEquals( expected[i], actual[i] );
664         }
665 
666         // test delimiter multichar
667         input = "1::2:::3::::4";
668         expected = new String[] { "1", "2", ":3", "", "4" };
669 
670         actual = StringUtils.splitByWholeSeparatorPreserveAllTokens( input, "::", -1 ) ;
671         assertEquals( expected.length, actual.length ) ;
672         for ( int i = 0 ; i < actual.length ; i+= 1 ) {
673             assertEquals( expected[i], actual[i] );
674         }
675 
676         // test delimiter char with max
677         input = "1::2::3:4";
678         expected = new String[] { "1", "", "2", ":3:4" };
679 
680         actual = StringUtils.splitByWholeSeparatorPreserveAllTokens( input, ":", 4 ) ;
681         assertEquals( expected.length, actual.length ) ;
682         for ( int i = 0 ; i < actual.length ; i+= 1 ) {
683             assertEquals( expected[i], actual[i] );
684         }
685     }
686     
687     @Test
688     public void testSplitPreserveAllTokens_String() {
689         assertNull(StringUtils.splitPreserveAllTokens(null));
690         assertEquals(0, StringUtils.splitPreserveAllTokens("").length);
691         
692         String str = "abc def";
693         String[] res = StringUtils.splitPreserveAllTokens(str);
694         assertEquals(2, res.length);
695         assertEquals("abc", res[0]);
696         assertEquals("def", res[1]);
697         
698         str = "abc  def";
699         res = StringUtils.splitPreserveAllTokens(str);
700         assertEquals(3, res.length);
701         assertEquals("abc", res[0]);
702         assertEquals("", res[1]);
703         assertEquals("def", res[2]);
704         
705         str = " abc ";
706         res = StringUtils.splitPreserveAllTokens(str);
707         assertEquals(3, res.length);
708         assertEquals("", res[0]);
709         assertEquals("abc", res[1]);
710         assertEquals("", res[2]);
711         
712         str = "a b .c";
713         res = StringUtils.splitPreserveAllTokens(str);
714         assertEquals(3, res.length);
715         assertEquals("a", res[0]);
716         assertEquals("b", res[1]);
717         assertEquals(".c", res[2]);
718         
719         str = " a b .c";
720         res = StringUtils.splitPreserveAllTokens(str);
721         assertEquals(4, res.length);
722         assertEquals("", res[0]);
723         assertEquals("a", res[1]);
724         assertEquals("b", res[2]);
725         assertEquals(".c", res[3]);
726         
727         str = "a  b  .c";
728         res = StringUtils.splitPreserveAllTokens(str);
729         assertEquals(5, res.length);
730         assertEquals("a", res[0]);
731         assertEquals("", res[1]);
732         assertEquals("b", res[2]);
733         assertEquals("", res[3]);
734         assertEquals(".c", res[4]);
735         
736         str = " a  ";
737         res = StringUtils.splitPreserveAllTokens(str);
738         assertEquals(4, res.length);
739         assertEquals("", res[0]);
740         assertEquals("a", res[1]);
741         assertEquals("", res[2]);
742         assertEquals("", res[3]);
743 
744         str = " a  b";
745         res = StringUtils.splitPreserveAllTokens(str);
746         assertEquals(4, res.length);
747         assertEquals("", res[0]);
748         assertEquals("a", res[1]);
749         assertEquals("", res[2]);
750         assertEquals("b", res[3]);
751 
752         str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
753         res = StringUtils.splitPreserveAllTokens(str);
754         assertEquals(WHITESPACE.length() + 1, res.length);
755         assertEquals("a", res[0]);
756         for(int i = 1; i < WHITESPACE.length()-1; i++)
757         {
758           assertEquals("", res[i]);
759         }
760         assertEquals("b" + NON_WHITESPACE + "c", res[WHITESPACE.length()]);                       
761     }
762     
763     @Test
764     public void testSplitPreserveAllTokens_StringChar() {
765         assertNull(StringUtils.splitPreserveAllTokens(null, '.'));
766         assertEquals(0, StringUtils.splitPreserveAllTokens("", '.').length);
767 
768         String str = "a.b. c";
769         String[] res = StringUtils.splitPreserveAllTokens(str, '.');
770         assertEquals(3, res.length);
771         assertEquals("a", res[0]);
772         assertEquals("b", res[1]);
773         assertEquals(" c", res[2]);
774             
775         str = "a.b.. c";
776         res = StringUtils.splitPreserveAllTokens(str, '.');
777         assertEquals(4, res.length);
778         assertEquals("a", res[0]);
779         assertEquals("b", res[1]);
780         assertEquals("", res[2]);
781         assertEquals(" c", res[3]);
782 
783         str = ".a.";
784         res = StringUtils.splitPreserveAllTokens(str, '.');
785         assertEquals(3, res.length);
786         assertEquals("", res[0]);
787         assertEquals("a", res[1]);
788         assertEquals("", res[2]);
789        
790         str = ".a..";
791         res = StringUtils.splitPreserveAllTokens(str, '.');
792         assertEquals(4, res.length);
793         assertEquals("", res[0]);
794         assertEquals("a", res[1]);
795         assertEquals("", res[2]);
796         assertEquals("", res[3]);
797         
798         str = "..a.";
799         res = StringUtils.splitPreserveAllTokens(str, '.');
800         assertEquals(4, res.length);
801         assertEquals("", res[0]);
802         assertEquals("", res[1]);
803         assertEquals("a", res[2]);
804         assertEquals("", res[3]);
805         
806         str = "..a";
807         res = StringUtils.splitPreserveAllTokens(str, '.');
808         assertEquals(3, res.length);
809         assertEquals("", res[0]);
810         assertEquals("", res[1]);
811         assertEquals("a", res[2]);
812         
813         str = "a b c";
814         res = StringUtils.splitPreserveAllTokens(str,' ');
815         assertEquals(3, res.length);
816         assertEquals("a", res[0]);
817         assertEquals("b", res[1]);
818         assertEquals("c", res[2]);
819 
820         str = "a  b  c";
821         res = StringUtils.splitPreserveAllTokens(str,' ');
822         assertEquals(5, res.length);
823         assertEquals("a", res[0]);
824         assertEquals("", res[1]);
825         assertEquals("b", res[2]);
826         assertEquals("", res[3]);
827         assertEquals("c", res[4]);
828         
829         str = " a b c";
830         res = StringUtils.splitPreserveAllTokens(str,' ');
831         assertEquals(4, res.length);
832         assertEquals("", res[0]);
833         assertEquals("a", res[1]);
834         assertEquals("b", res[2]);
835         assertEquals("c", res[3]);
836 
837         str = "  a b c";
838         res = StringUtils.splitPreserveAllTokens(str,' ');
839         assertEquals(5, res.length);
840         assertEquals("", res[0]);
841         assertEquals("", res[1]);
842         assertEquals("a", res[2]);
843         assertEquals("b", res[3]);
844         assertEquals("c", res[4]);
845 
846         str = "a b c ";
847         res = StringUtils.splitPreserveAllTokens(str,' ');
848         assertEquals(4, res.length);
849         assertEquals("a", res[0]);
850         assertEquals("b", res[1]);
851         assertEquals("c", res[2]);
852         assertEquals("", res[3]);
853 
854         str = "a b c  ";
855         res = StringUtils.splitPreserveAllTokens(str,' ');
856         assertEquals(5, res.length);
857         assertEquals("a", res[0]);
858         assertEquals("b", res[1]);
859         assertEquals("c", res[2]);
860         assertEquals("", res[3]);
861         assertEquals("", res[3]);
862 
863         // Match example in javadoc
864         {
865           String[] results;
866           final String[] expectedResults = {"a", "", "b", "c"};
867           results = StringUtils.splitPreserveAllTokens("a..b.c",'.');
868           assertEquals(expectedResults.length, results.length);
869           for (int i = 0; i < expectedResults.length; i++) {
870               assertEquals(expectedResults[i], results[i]);
871           }
872         }
873     }
874     
875     @Test
876     public void testSplitPreserveAllTokens_StringString_StringStringInt() {
877         assertNull(StringUtils.splitPreserveAllTokens(null, "."));
878         assertNull(StringUtils.splitPreserveAllTokens(null, ".", 3));
879         
880         assertEquals(0, StringUtils.splitPreserveAllTokens("", ".").length);
881         assertEquals(0, StringUtils.splitPreserveAllTokens("", ".", 3).length);
882         
883         innerTestSplitPreserveAllTokens('.', ".", ' ');
884         innerTestSplitPreserveAllTokens('.', ".", ',');
885         innerTestSplitPreserveAllTokens('.', ".,", 'x');
886         for (int i = 0; i < WHITESPACE.length(); i++) {
887             for (int j = 0; j < NON_WHITESPACE.length(); j++) {
888                 innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j));
889                 innerTestSplitPreserveAllTokens(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j));
890             }
891         }
892 
893         {
894           String[] results;
895           final String[] expectedResults = {"ab", "de fg"};
896           results = StringUtils.splitPreserveAllTokens("ab de fg", null, 2);
897           assertEquals(expectedResults.length, results.length);
898           for (int i = 0; i < expectedResults.length; i++) {
899               assertEquals(expectedResults[i], results[i]);
900           }
901         }
902 
903         {
904           String[] results;
905           final String[] expectedResults = {"ab", "  de fg"};
906           results = StringUtils.splitPreserveAllTokens("ab   de fg", null, 2);
907           assertEquals(expectedResults.length, results.length);
908           for (int i = 0; i < expectedResults.length; i++) {
909               assertEquals(expectedResults[i], results[i]);
910           }
911         }
912         
913         {
914           String[] results;
915           final String[] expectedResults = {"ab", "::de:fg"};
916           results = StringUtils.splitPreserveAllTokens("ab:::de:fg", ":", 2);
917           assertEquals(expectedResults.length, results.length);
918           for (int i = 0; i < expectedResults.length; i++) {
919               assertEquals(expectedResults[i], results[i]);
920           }
921         }
922         
923         {
924           String[] results;
925           final String[] expectedResults = {"ab", "", " de fg"};
926           results = StringUtils.splitPreserveAllTokens("ab   de fg", null, 3);
927           assertEquals(expectedResults.length, results.length);
928           for (int i = 0; i < expectedResults.length; i++) {
929               assertEquals(expectedResults[i], results[i]);
930           }
931         }
932         
933         {
934           String[] results;
935           final String[] expectedResults = {"ab", "", "", "de fg"};
936           results = StringUtils.splitPreserveAllTokens("ab   de fg", null, 4);
937           assertEquals(expectedResults.length, results.length);
938           for (int i = 0; i < expectedResults.length; i++) {
939               assertEquals(expectedResults[i], results[i]);
940           }
941         }
942 
943         {
944           final String[] expectedResults = {"ab", "cd:ef"};
945           String[] results;
946           results = StringUtils.splitPreserveAllTokens("ab:cd:ef",":", 2);
947           assertEquals(expectedResults.length, results.length);
948           for (int i = 0; i < expectedResults.length; i++) {
949               assertEquals(expectedResults[i], results[i]);
950           }
951         }
952 
953         {
954           String[] results;
955           final String[] expectedResults = {"ab", ":cd:ef"};
956           results = StringUtils.splitPreserveAllTokens("ab::cd:ef",":", 2);
957           assertEquals(expectedResults.length, results.length);
958           for (int i = 0; i < expectedResults.length; i++) {
959               assertEquals(expectedResults[i], results[i]);
960           }
961         }
962 
963         {
964           String[] results;
965           final String[] expectedResults = {"ab", "", ":cd:ef"};
966           results = StringUtils.splitPreserveAllTokens("ab:::cd:ef",":", 3);
967           assertEquals(expectedResults.length, results.length);
968           for (int i = 0; i < expectedResults.length; i++) {
969               assertEquals(expectedResults[i], results[i]);
970           }
971         }
972 
973         {
974           String[] results;
975           final String[] expectedResults = {"ab", "", "", "cd:ef"};
976           results = StringUtils.splitPreserveAllTokens("ab:::cd:ef",":", 4);
977           assertEquals(expectedResults.length, results.length);
978           for (int i = 0; i < expectedResults.length; i++) {
979               assertEquals(expectedResults[i], results[i]);
980           }
981         }
982 
983         {
984           String[] results;
985           final String[] expectedResults = {"", "ab", "", "", "cd:ef"};
986           results = StringUtils.splitPreserveAllTokens(":ab:::cd:ef",":", 5);
987           assertEquals(expectedResults.length, results.length);
988           for (int i = 0; i < expectedResults.length; i++) {
989               assertEquals(expectedResults[i], results[i]);
990           }
991         }
992         
993         {
994           String[] results;
995           final String[] expectedResults = {"", "", "ab", "", "", "cd:ef"};
996           results = StringUtils.splitPreserveAllTokens("::ab:::cd:ef",":", 6);
997           assertEquals(expectedResults.length, results.length);
998           for (int i = 0; i < expectedResults.length; i++) {
999               assertEquals(expectedResults[i], results[i]);
1000           }
1001         }
1002         
1003     }
1004     
1005     private void innerTestSplitPreserveAllTokens(final char separator, final String sepStr, final char noMatch) {
1006         final String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
1007             "), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
1008         
1009         final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
1010         String[] res;
1011         // (str, sepStr)
1012         res = StringUtils.splitPreserveAllTokens(str, sepStr);
1013         assertEquals(msg, 4, res.length);
1014         assertEquals(msg, "a", res[0]);
1015         assertEquals(msg, "b", res[1]);
1016         assertEquals(msg, "", res[2]);
1017         assertEquals(msg, noMatch + "c", res[3]);
1018         
1019         final String str2 = separator + "a" + separator;
1020         res = StringUtils.splitPreserveAllTokens(str2, sepStr);
1021         assertEquals(msg, 3, res.length);
1022         assertEquals(msg, "", res[0]);
1023         assertEquals(msg, "a", res[1]);
1024         assertEquals(msg, "", res[2]);
1025 
1026         res = StringUtils.splitPreserveAllTokens(str, sepStr, -1);
1027         assertEquals(msg, 4, res.length);
1028         assertEquals(msg, "a", res[0]);
1029         assertEquals(msg, "b", res[1]);
1030         assertEquals(msg, "", res[2]);
1031         assertEquals(msg, noMatch + "c", res[3]);
1032         
1033         res = StringUtils.splitPreserveAllTokens(str, sepStr, 0);
1034         assertEquals(msg, 4, res.length);
1035         assertEquals(msg, "a", res[0]);
1036         assertEquals(msg, "b", res[1]);
1037         assertEquals(msg, "", res[2]);
1038         assertEquals(msg, noMatch + "c", res[3]);
1039         
1040         res = StringUtils.splitPreserveAllTokens(str, sepStr, 1);
1041         assertEquals(msg, 1, res.length);
1042         assertEquals(msg, str, res[0]);
1043         
1044         res = StringUtils.splitPreserveAllTokens(str, sepStr, 2);
1045         assertEquals(msg, 2, res.length);
1046         assertEquals(msg, "a", res[0]);
1047         assertEquals(msg, str.substring(2), res[1]);
1048     }
1049 
1050     @Test
1051     public void testSplitByCharacterType() {
1052         assertNull(StringUtils.splitByCharacterType(null));
1053         assertEquals(0, StringUtils.splitByCharacterType("").length);
1054         
1055         assertTrue(ArrayUtils.isEquals(new String[] { "ab", " ", "de", " ",
1056         "fg" }, StringUtils.splitByCharacterType("ab de fg")));
1057         
1058         assertTrue(ArrayUtils.isEquals(new String[] { "ab", "   ", "de", " ",
1059         "fg" }, StringUtils.splitByCharacterType("ab   de fg")));
1060         
1061         assertTrue(ArrayUtils.isEquals(new String[] { "ab", ":", "cd", ":",
1062         "ef" }, StringUtils.splitByCharacterType("ab:cd:ef")));
1063         
1064         assertTrue(ArrayUtils.isEquals(new String[] { "number", "5" },
1065                 StringUtils.splitByCharacterType("number5")));
1066         
1067         assertTrue(ArrayUtils.isEquals(new String[] { "foo", "B", "ar" },
1068                 StringUtils.splitByCharacterType("fooBar")));
1069         
1070         assertTrue(ArrayUtils.isEquals(new String[] { "foo", "200", "B", "ar" },
1071                 StringUtils.splitByCharacterType("foo200Bar")));
1072         
1073         assertTrue(ArrayUtils.isEquals(new String[] { "ASFR", "ules" },
1074                 StringUtils.splitByCharacterType("ASFRules")));
1075     }
1076     
1077     @Test
1078     public void testSplitByCharacterTypeCamelCase() {
1079         assertNull(StringUtils.splitByCharacterTypeCamelCase(null));
1080         assertEquals(0, StringUtils.splitByCharacterTypeCamelCase("").length);
1081 
1082         assertTrue(ArrayUtils.isEquals(new String[] { "ab", " ", "de", " ",
1083                 "fg" }, StringUtils.splitByCharacterTypeCamelCase("ab de fg")));
1084 
1085         assertTrue(ArrayUtils.isEquals(new String[] { "ab", "   ", "de", " ",
1086                 "fg" }, StringUtils.splitByCharacterTypeCamelCase("ab   de fg")));
1087 
1088         assertTrue(ArrayUtils.isEquals(new String[] { "ab", ":", "cd", ":",
1089                 "ef" }, StringUtils.splitByCharacterTypeCamelCase("ab:cd:ef")));
1090         
1091         assertTrue(ArrayUtils.isEquals(new String[] { "number", "5" },
1092                 StringUtils.splitByCharacterTypeCamelCase("number5")));
1093 
1094         assertTrue(ArrayUtils.isEquals(new String[] { "foo", "Bar" },
1095                 StringUtils.splitByCharacterTypeCamelCase("fooBar")));
1096 
1097         assertTrue(ArrayUtils.isEquals(new String[] { "foo", "200", "Bar" },
1098                 StringUtils.splitByCharacterTypeCamelCase("foo200Bar")));
1099 
1100         assertTrue(ArrayUtils.isEquals(new String[] { "ASF", "Rules" },
1101                 StringUtils.splitByCharacterTypeCamelCase("ASFRules")));
1102     }
1103 
1104     @Test
1105     public void testDeleteWhitespace_String() {
1106         assertNull(StringUtils.deleteWhitespace(null));
1107         assertEquals("", StringUtils.deleteWhitespace(""));
1108         assertEquals("", StringUtils.deleteWhitespace("  \u000C  \t\t\u001F\n\n \u000B  "));
1109         assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
1110         assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
1111         // Note: u-2007 and u-000A both cause problems in the source code
1112         // it should ignore 2007 but delete 000A
1113         assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("  \u00A0  \t\t\n\n \u202F  "));
1114         assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
1115         assertEquals("test", StringUtils.deleteWhitespace("\u000Bt  \t\n\u0009e\rs\n\n   \tt"));
1116     }
1117 
1118     @Test
1119     public void testLang623() {
1120         assertEquals("t", StringUtils.replaceChars("\u00DE", '\u00DE', 't'));
1121         assertEquals("t", StringUtils.replaceChars("\u00FE", '\u00FE', 't'));
1122     }
1123 
1124     @Test
1125     public void testReplace_StringStringString() {
1126         assertNull(StringUtils.replace(null, null, null));
1127         assertNull(StringUtils.replace(null, null, "any"));
1128         assertNull(StringUtils.replace(null, "any", null));
1129         assertNull(StringUtils.replace(null, "any", "any"));
1130 
1131         assertEquals("", StringUtils.replace("", null, null));
1132         assertEquals("", StringUtils.replace("", null, "any"));
1133         assertEquals("", StringUtils.replace("", "any", null));
1134         assertEquals("", StringUtils.replace("", "any", "any"));
1135 
1136         assertEquals("FOO", StringUtils.replace("FOO", "", "any"));
1137         assertEquals("FOO", StringUtils.replace("FOO", null, "any"));
1138         assertEquals("FOO", StringUtils.replace("FOO", "F", null));
1139         assertEquals("FOO", StringUtils.replace("FOO", null, null));
1140 
1141         assertEquals("", StringUtils.replace("foofoofoo", "foo", ""));
1142         assertEquals("barbarbar", StringUtils.replace("foofoofoo", "foo", "bar"));
1143         assertEquals("farfarfar", StringUtils.replace("foofoofoo", "oo", "ar"));
1144     }
1145     
1146     @Test
1147     public void testReplacePattern() {
1148         assertEquals("X", StringUtils.replacePattern("<A>\nxy\n</A>", "<A>.*</A>", "X"));
1149     }
1150     
1151     @Test
1152     public void testRemovePattern() {
1153         assertEquals("", StringUtils.removePattern("<A>x\\ny</A>", "<A>.*</A>"));
1154     }
1155     
1156     @Test
1157     public void testReplace_StringStringStringInt() {
1158         assertNull(StringUtils.replace(null, null, null, 2));
1159         assertNull(StringUtils.replace(null, null, "any", 2));
1160         assertNull(StringUtils.replace(null, "any", null, 2));
1161         assertNull(StringUtils.replace(null, "any", "any", 2));
1162 
1163         assertEquals("", StringUtils.replace("", null, null, 2));
1164         assertEquals("", StringUtils.replace("", null, "any", 2));
1165         assertEquals("", StringUtils.replace("", "any", null, 2));
1166         assertEquals("", StringUtils.replace("", "any", "any", 2));
1167         
1168         final String str = new String(new char[] {'o', 'o', 'f', 'o', 'o'});
1169         assertSame(str, StringUtils.replace(str, "x", "", -1));
1170         
1171         assertEquals("f", StringUtils.replace("oofoo", "o", "", -1));
1172         assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0));
1173         assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1));
1174         assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2));
1175         assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3));
1176         assertEquals("f", StringUtils.replace("oofoo", "o", "", 4));
1177         
1178         assertEquals("f", StringUtils.replace("oofoo", "o", "", -5));
1179         assertEquals("f", StringUtils.replace("oofoo", "o", "", 1000));
1180     }
1181     
1182     @Test
1183     public void testReplaceOnce_StringStringString() {
1184         assertNull(StringUtils.replaceOnce(null, null, null));
1185         assertNull(StringUtils.replaceOnce(null, null, "any"));
1186         assertNull(StringUtils.replaceOnce(null, "any", null));
1187         assertNull(StringUtils.replaceOnce(null, "any", "any"));
1188 
1189         assertEquals("", StringUtils.replaceOnce("", null, null));
1190         assertEquals("", StringUtils.replaceOnce("", null, "any"));
1191         assertEquals("", StringUtils.replaceOnce("", "any", null));
1192         assertEquals("", StringUtils.replaceOnce("", "any", "any"));
1193 
1194         assertEquals("FOO", StringUtils.replaceOnce("FOO", "", "any"));
1195         assertEquals("FOO", StringUtils.replaceOnce("FOO", null, "any"));
1196         assertEquals("FOO", StringUtils.replaceOnce("FOO", "F", null));
1197         assertEquals("FOO", StringUtils.replaceOnce("FOO", null, null));
1198 
1199         assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", ""));
1200     }
1201 
1202     /**
1203      * Test method for 'StringUtils.replaceEach(String, String[], String[])'
1204      */
1205     @Test
1206     public void testReplace_StringStringArrayStringArray() {
1207         //JAVADOC TESTS START
1208         assertNull(StringUtils.replaceEach(null, new String[]{"a"}, new String[]{"b"}));
1209         assertEquals(StringUtils.replaceEach("", new String[]{"a"}, new String[]{"b"}),"");
1210         assertEquals(StringUtils.replaceEach("aba", null, null),"aba");
1211         assertEquals(StringUtils.replaceEach("aba", new String[0], null),"aba");
1212         assertEquals(StringUtils.replaceEach("aba", null, new String[0]),"aba");
1213         assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, null),"aba");
1214 
1215         assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}),"b");
1216         assertEquals(StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}),"aba");
1217         assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}),"wcte");
1218         assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}),"dcte");
1219         //JAVADOC TESTS END
1220 
1221         assertEquals("bcc", StringUtils.replaceEach("abc", new String[]{"a", "b"}, new String[]{"b", "c"}));
1222         assertEquals("q651.506bera", StringUtils.replaceEach("d216.102oren",
1223             new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 
1224                 "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", 
1225                 "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 
1226                 "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
1227             new String[]{"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", 
1228                 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "N", "O", "P", "Q", 
1229                 "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", 
1230                 "H", "I", "J", "K", "L", "M", "5", "6", "7", "8", "9", "1", "2", "3", "4"}));
1231 
1232         // Test null safety inside arrays - LANG-552
1233         assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{null}),"aba");
1234         assertEquals(StringUtils.replaceEach("aba", new String[]{"a", "b"}, new String[]{"c", null}),"cbc");
1235     }
1236 
1237     /**
1238      * Test method for 'StringUtils.replaceEachRepeatedly(String, String[], String[])'
1239      */
1240     @Test
1241     public void testReplace_StringStringArrayStringArrayBoolean() {
1242         //JAVADOC TESTS START
1243         assertNull(StringUtils.replaceEachRepeatedly(null, new String[]{"a"}, new String[]{"b"}));
1244         assertEquals(StringUtils.replaceEachRepeatedly("", new String[]{"a"}, new String[]{"b"}),"");
1245         assertEquals(StringUtils.replaceEachRepeatedly("aba", null, null),"aba");
1246         assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], null),"aba");
1247         assertEquals(StringUtils.replaceEachRepeatedly("aba", null, new String[0]),"aba");
1248         assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], null),"aba");
1249 
1250         assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}),"b");
1251         assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}),"aba");
1252         assertEquals(StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}),"wcte");
1253         assertEquals(StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}),"tcte");
1254 
1255         try {
1256             StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"});
1257             fail("Should be a circular reference");
1258         } catch (final IllegalStateException e) {}
1259 
1260         //JAVADOC TESTS END
1261     }
1262     
1263     @Test
1264     public void testReplaceChars_StringCharChar() {
1265         assertNull(StringUtils.replaceChars(null, 'b', 'z'));
1266         assertEquals("", StringUtils.replaceChars("", 'b', 'z'));
1267         assertEquals("azcza", StringUtils.replaceChars("abcba", 'b', 'z'));
1268         assertEquals("abcba", StringUtils.replaceChars("abcba", 'x', 'z'));
1269     }
1270     
1271     @Test
1272     public void testReplaceChars_StringStringString() {
1273         assertNull(StringUtils.replaceChars(null, null, null));
1274         assertNull(StringUtils.replaceChars(null, "", null));
1275         assertNull(StringUtils.replaceChars(null, "a", null));
1276         assertNull(StringUtils.replaceChars(null, null, ""));
1277         assertNull(StringUtils.replaceChars(null, null, "x"));
1278         
1279         assertEquals("", StringUtils.replaceChars("", null, null));
1280         assertEquals("", StringUtils.replaceChars("", "", null));
1281         assertEquals("", StringUtils.replaceChars("", "a", null));
1282         assertEquals("", StringUtils.replaceChars("", null, ""));
1283         assertEquals("", StringUtils.replaceChars("", null, "x"));
1284 
1285         assertEquals("abc", StringUtils.replaceChars("abc", null, null));
1286         assertEquals("abc", StringUtils.replaceChars("abc", null, ""));
1287         assertEquals("abc", StringUtils.replaceChars("abc", null, "x"));
1288         
1289         assertEquals("abc", StringUtils.replaceChars("abc", "", null));
1290         assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
1291         assertEquals("abc", StringUtils.replaceChars("abc", "", "x"));
1292         
1293         assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
1294         assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
1295         assertEquals("axc", StringUtils.replaceChars("abc", "b", "x"));
1296         
1297         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
1298         assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
1299         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
1300         
1301         assertEquals("abcba", StringUtils.replaceChars("abcba", "z", "w"));
1302         assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
1303         
1304         // Javadoc examples:
1305         assertEquals("jelly", StringUtils.replaceChars("hello", "ho", "jy"));
1306         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
1307         assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
1308         assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
1309         
1310         // From http://issues.apache.org/bugzilla/show_bug.cgi?id=25454
1311         assertEquals("bcc", StringUtils.replaceChars("abc", "ab", "bc"));
1312         assertEquals("q651.506bera", StringUtils.replaceChars("d216.102oren",
1313             "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
1314             "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234"));
1315     }
1316     
1317     @Test
1318     public void testOverlay_StringStringIntInt() {
1319         assertNull(StringUtils.overlay(null, null, 2, 4));
1320         assertNull(StringUtils.overlay(null, null, -2, -4));
1321         
1322         assertEquals("", StringUtils.overlay("", null, 0, 0));
1323         assertEquals("", StringUtils.overlay("", "", 0, 0));
1324         assertEquals("zzzz", StringUtils.overlay("", "zzzz", 0, 0));
1325         assertEquals("zzzz", StringUtils.overlay("", "zzzz", 2, 4));
1326         assertEquals("zzzz", StringUtils.overlay("", "zzzz", -2, -4));
1327         
1328         assertEquals("abef", StringUtils.overlay("abcdef", null, 2, 4));
1329         assertEquals("abef", StringUtils.overlay("abcdef", null, 4, 2));
1330         assertEquals("abef", StringUtils.overlay("abcdef", "", 2, 4));
1331         assertEquals("abef", StringUtils.overlay("abcdef", "", 4, 2));
1332         assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 2, 4));
1333         assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 4, 2));
1334         
1335         assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", -1, 4));
1336         assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", 4, -1));
1337         assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -2, -1));
1338         assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -1, -2));
1339         assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 4, 10));
1340         assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 4));
1341         assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 8, 10));
1342         assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 8));
1343     }
1344 
1345     @Test
1346     public void testRepeat_StringInt() {
1347         assertNull(StringUtils.repeat(null, 2));
1348         assertEquals("", StringUtils.repeat("ab", 0));
1349         assertEquals("", StringUtils.repeat("", 3));
1350         assertEquals("aaa", StringUtils.repeat("a", 3));
1351         assertEquals("ababab", StringUtils.repeat("ab", 3));
1352         assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
1353         final String str = StringUtils.repeat("a", 10000);  // bigger than pad limit
1354         assertEquals(10000, str.length());
1355         assertTrue(StringUtils.containsOnly(str, new char[] {'a'}));
1356     }
1357 
1358     @Test
1359     public void testRepeat_StringStringInt() {
1360         assertNull(StringUtils.repeat(null, null, 2));
1361         assertNull(StringUtils.repeat(null, "x", 2));
1362         assertEquals("", StringUtils.repeat("", null, 2));
1363 
1364         assertEquals("", StringUtils.repeat("ab", "", 0));
1365         assertEquals("", StringUtils.repeat("", "", 2));
1366 
1367         assertEquals("xx", StringUtils.repeat("", "x", 3));
1368 
1369         assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
1370     }
1371 
1372     @Test
1373     public void testChop() {
1374 
1375         final String[][] chopCases = {
1376             { FOO_UNCAP + "\r\n", FOO_UNCAP } ,
1377             { FOO_UNCAP + "\n" , FOO_UNCAP } ,
1378             { FOO_UNCAP + "\r", FOO_UNCAP },
1379             { FOO_UNCAP + " \r", FOO_UNCAP + " " },
1380             { "foo", "fo"},
1381             { "foo\nfoo", "foo\nfo" },
1382             { "\n", "" },
1383             { "\r", "" },
1384             { "\r\n", "" },
1385             { null, null },
1386             { "", "" },
1387             { "a", "" },
1388         };
1389         for (final String[] chopCase : chopCases) {
1390             final String original = chopCase[0];
1391             final String expectedResult = chopCase[1];
1392             assertEquals("chop(String) failed",
1393                     expectedResult, StringUtils.chop(original));
1394         }
1395     }
1396 
1397     @Test
1398     public void testChomp() {
1399 
1400         final String[][] chompCases = {
1401             { FOO_UNCAP + "\r\n", FOO_UNCAP },
1402             { FOO_UNCAP + "\n" , FOO_UNCAP },
1403             { FOO_UNCAP + "\r", FOO_UNCAP },
1404             { FOO_UNCAP + " \r", FOO_UNCAP + " " },
1405             { FOO_UNCAP, FOO_UNCAP },
1406             { FOO_UNCAP + "\n\n", FOO_UNCAP + "\n"},
1407             { FOO_UNCAP + "\r\n\r\n", FOO_UNCAP + "\r\n" },
1408             { "foo\nfoo", "foo\nfoo" },
1409             { "foo\n\rfoo", "foo\n\rfoo" },
1410             { "\n", "" },
1411             { "\r", "" },
1412             { "a", "a" },
1413             { "\r\n", "" },
1414             { "", "" },
1415             { null, null },
1416             { FOO_UNCAP + "\n\r", FOO_UNCAP + "\n"}
1417         };
1418         for (final String[] chompCase : chompCases) {
1419             final String original = chompCase[0];
1420             final String expectedResult = chompCase[1];
1421             assertEquals("chomp(String) failed",
1422                     expectedResult, StringUtils.chomp(original));
1423         }
1424 
1425         assertEquals("chomp(String, String) failed",
1426                 "foo", StringUtils.chomp("foobar", "bar"));
1427         assertEquals("chomp(String, String) failed",
1428                 "foobar", StringUtils.chomp("foobar", "baz"));
1429         assertEquals("chomp(String, String) failed",
1430                 "foo", StringUtils.chomp("foo", "foooo"));
1431         assertEquals("chomp(String, String) failed",
1432                 "foobar", StringUtils.chomp("foobar", ""));
1433         assertEquals("chomp(String, String) failed",
1434                 "foobar", StringUtils.chomp("foobar", null));
1435         assertEquals("chomp(String, String) failed",
1436                 "", StringUtils.chomp("", "foo"));
1437         assertEquals("chomp(String, String) failed",
1438                 "", StringUtils.chomp("", null));
1439         assertEquals("chomp(String, String) failed",
1440                 "", StringUtils.chomp("", ""));
1441         assertEquals("chomp(String, String) failed",
1442                 null, StringUtils.chomp(null, "foo"));
1443         assertEquals("chomp(String, String) failed",
1444                 null, StringUtils.chomp(null, null));
1445         assertEquals("chomp(String, String) failed",
1446                 null, StringUtils.chomp(null, ""));
1447         assertEquals("chomp(String, String) failed",
1448                 "", StringUtils.chomp("foo", "foo"));
1449         assertEquals("chomp(String, String) failed",
1450                 " ", StringUtils.chomp(" foo", "foo"));
1451         assertEquals("chomp(String, String) failed",
1452                 "foo ", StringUtils.chomp("foo ", "foo"));
1453     }
1454 
1455     //-----------------------------------------------------------------------
1456     @Test
1457     public void testRightPad_StringInt() {
1458         assertNull(StringUtils.rightPad(null, 5));
1459         assertEquals("     ", StringUtils.rightPad("", 5));
1460         assertEquals("abc  ", StringUtils.rightPad("abc", 5));
1461         assertEquals("abc", StringUtils.rightPad("abc", 2));
1462         assertEquals("abc", StringUtils.rightPad("abc", -1));
1463     }
1464 
1465     @Test
1466     public void testRightPad_StringIntChar() {
1467         assertNull(StringUtils.rightPad(null, 5, ' '));
1468         assertEquals("     ", StringUtils.rightPad("", 5, ' '));
1469         assertEquals("abc  ", StringUtils.rightPad("abc", 5, ' '));
1470         assertEquals("abc", StringUtils.rightPad("abc", 2, ' '));
1471         assertEquals("abc", StringUtils.rightPad("abc", -1, ' '));
1472         assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x'));
1473         final String str = StringUtils.rightPad("aaa", 10000, 'a');  // bigger than pad length
1474         assertEquals(10000, str.length());
1475         assertTrue(StringUtils.containsOnly(str, new char[] {'a'}));
1476     }
1477 
1478     @Test
1479     public void testRightPad_StringIntString() {
1480         assertNull(StringUtils.rightPad(null, 5, "-+"));
1481         assertEquals("     ", StringUtils.rightPad("", 5, " "));
1482         assertNull(StringUtils.rightPad(null, 8, null));
1483         assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+"));
1484         assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~"));
1485         assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
1486         assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
1487         assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
1488         assertEquals("abc  ", StringUtils.rightPad("abc", 5, null));
1489         assertEquals("abc  ", StringUtils.rightPad("abc", 5, ""));
1490     }
1491         
1492     //-----------------------------------------------------------------------
1493     @Test
1494     public void testLeftPad_StringInt() {
1495         assertNull(StringUtils.leftPad(null, 5));
1496         assertEquals("     ", StringUtils.leftPad("", 5));
1497         assertEquals("  abc", StringUtils.leftPad("abc", 5));
1498         assertEquals("abc", StringUtils.leftPad("abc", 2));
1499     }
1500         
1501     @Test
1502     public void testLeftPad_StringIntChar() {
1503         assertNull(StringUtils.leftPad(null, 5, ' '));
1504         assertEquals("     ", StringUtils.leftPad("", 5, ' '));
1505         assertEquals("  abc", StringUtils.leftPad("abc", 5, ' '));
1506         assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x'));
1507         assertEquals("\uffff\uffffabc", StringUtils.leftPad("abc", 5, '\uffff'));
1508         assertEquals("abc", StringUtils.leftPad("abc", 2, ' '));
1509         final String str = StringUtils.leftPad("aaa", 10000, 'a');  // bigger than pad length
1510         assertEquals(10000, str.length());
1511         assertTrue(StringUtils.containsOnly(str, new char[] {'a'}));
1512     }
1513         
1514     @Test
1515     public void testLeftPad_StringIntString() {
1516         assertNull(StringUtils.leftPad(null, 5, "-+"));
1517         assertNull(StringUtils.leftPad(null, 5, null));
1518         assertEquals("     ", StringUtils.leftPad("", 5, " "));
1519         assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+"));
1520         assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~"));
1521         assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
1522         assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
1523         assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
1524         assertEquals("  abc", StringUtils.leftPad("abc", 5, null));
1525         assertEquals("  abc", StringUtils.leftPad("abc", 5, ""));
1526     }
1527 
1528     @Test
1529     public void testLengthString() {
1530         assertEquals(0, StringUtils.length(null));
1531         assertEquals(0, StringUtils.length(""));
1532         assertEquals(0, StringUtils.length(StringUtils.EMPTY));
1533         assertEquals(1, StringUtils.length("A"));
1534         assertEquals(1, StringUtils.length(" "));
1535         assertEquals(8, StringUtils.length("ABCDEFGH"));
1536     }
1537 
1538     @Test
1539     public void testLengthStringBuffer() {
1540         assertEquals(0, StringUtils.length(new StringBuffer("")));
1541         assertEquals(0, StringUtils.length(new StringBuffer(StringUtils.EMPTY)));
1542         assertEquals(1, StringUtils.length(new StringBuffer("A")));
1543         assertEquals(1, StringUtils.length(new StringBuffer(" ")));
1544         assertEquals(8, StringUtils.length(new StringBuffer("ABCDEFGH")));
1545     }
1546 
1547     @Test
1548     public void testLengthStringBuilder() {
1549         assertEquals(0, StringUtils.length(new StringBuilder("")));
1550         assertEquals(0, StringUtils.length(new StringBuilder(StringUtils.EMPTY)));
1551         assertEquals(1, StringUtils.length(new StringBuilder("A")));
1552         assertEquals(1, StringUtils.length(new StringBuilder(" ")));
1553         assertEquals(8, StringUtils.length(new StringBuilder("ABCDEFGH")));
1554     }
1555     
1556     @Test
1557     public void testLength_CharBuffer() {
1558         assertEquals(0, StringUtils.length(CharBuffer.wrap("")));
1559         assertEquals(1, StringUtils.length(CharBuffer.wrap("A")));
1560         assertEquals(1, StringUtils.length(CharBuffer.wrap(" ")));
1561         assertEquals(8, StringUtils.length(CharBuffer.wrap("ABCDEFGH")));
1562     }
1563 
1564     //-----------------------------------------------------------------------
1565     @Test
1566     public void testCenter_StringInt() {
1567         assertNull(StringUtils.center(null, -1));
1568         assertNull(StringUtils.center(null, 4));
1569         assertEquals("    ", StringUtils.center("", 4));
1570         assertEquals("ab", StringUtils.center("ab", 0));
1571         assertEquals("ab", StringUtils.center("ab", -1));
1572         assertEquals("ab", StringUtils.center("ab", 1));
1573         assertEquals("    ", StringUtils.center("", 4));
1574         assertEquals(" ab ", StringUtils.center("ab", 4));
1575         assertEquals("abcd", StringUtils.center("abcd", 2));
1576         assertEquals(" a  ", StringUtils.center("a", 4));
1577         assertEquals("  a  ", StringUtils.center("a", 5));
1578     }
1579     
1580     @Test
1581     public void testCenter_StringIntChar() {
1582         assertNull(StringUtils.center(null, -1, ' '));
1583         assertNull(StringUtils.center(null, 4, ' '));
1584         assertEquals("    ", StringUtils.center("", 4, ' '));
1585         assertEquals("ab", StringUtils.center("ab", 0, ' '));
1586         assertEquals("ab", StringUtils.center("ab", -1, ' '));
1587         assertEquals("ab", StringUtils.center("ab", 1, ' '));
1588         assertEquals("    ", StringUtils.center("", 4, ' '));
1589         assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
1590         assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
1591         assertEquals(" a  ", StringUtils.center("a", 4, ' '));
1592         assertEquals("  a  ", StringUtils.center("a", 5, ' '));
1593         assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
1594     }
1595     
1596     @Test
1597     public void testCenter_StringIntString() {
1598         assertNull(StringUtils.center(null, 4, null));
1599         assertNull(StringUtils.center(null, -1, " "));
1600         assertNull(StringUtils.center(null, 4, " "));
1601         assertEquals("    ", StringUtils.center("", 4, " "));
1602         assertEquals("ab", StringUtils.center("ab", 0, " "));
1603         assertEquals("ab", StringUtils.center("ab", -1, " "));
1604         assertEquals("ab", StringUtils.center("ab", 1, " "));
1605         assertEquals("    ", StringUtils.center("", 4, " "));
1606         assertEquals(" ab ", StringUtils.center("ab", 4, " "));
1607         assertEquals("abcd", StringUtils.center("abcd", 2, " "));
1608         assertEquals(" a  ", StringUtils.center("a", 4, " "));
1609         assertEquals("yayz", StringUtils.center("a", 4, "yz"));
1610         assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
1611         assertEquals("  abc  ", StringUtils.center("abc", 7, null));
1612         assertEquals("  abc  ", StringUtils.center("abc", 7, ""));
1613     }
1614 
1615     //-----------------------------------------------------------------------
1616     @Test
1617     public void testReverse_String() {
1618         assertNull(StringUtils.reverse(null) );
1619         assertEquals("", StringUtils.reverse("") );
1620         assertEquals("sdrawkcab", StringUtils.reverse("backwards") );
1621     }
1622         
1623     @Test
1624     public void testReverseDelimited_StringChar() {
1625         assertNull(StringUtils.reverseDelimited(null, '.') );
1626         assertEquals("", StringUtils.reverseDelimited("", '.') );
1627         assertEquals("c.b.a", StringUtils.reverseDelimited("a.b.c", '.') );
1628         assertEquals("a b c", StringUtils.reverseDelimited("a b c", '.') );
1629         assertEquals("", StringUtils.reverseDelimited("", '.') );
1630     }
1631 
1632     //-----------------------------------------------------------------------
1633     @Test
1634     public void testDefault_String() {
1635         assertEquals("", StringUtils.defaultString(null));
1636         assertEquals("", StringUtils.defaultString(""));
1637         assertEquals("abc", StringUtils.defaultString("abc"));
1638     }
1639 
1640     @Test
1641     public void testDefault_StringString() {
1642         assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
1643         assertEquals("", StringUtils.defaultString("", "NULL"));
1644         assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
1645     }
1646 
1647     @Test
1648     public void testDefaultIfEmpty_StringString() {
1649         assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
1650         assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
1651         assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
1652         assertNull(StringUtils.defaultIfEmpty("", null));
1653         // Tests compatibility for the API return type
1654         final String s = StringUtils.defaultIfEmpty("abc", "NULL");
1655         assertEquals("abc", s);
1656     }
1657 
1658     @Test
1659     public void testDefaultIfBlank_StringString() {
1660         assertEquals("NULL", StringUtils.defaultIfBlank(null, "NULL"));
1661         assertEquals("NULL", StringUtils.defaultIfBlank("", "NULL"));
1662         assertEquals("NULL", StringUtils.defaultIfBlank(" ", "NULL"));
1663         assertEquals("abc", StringUtils.defaultIfBlank("abc", "NULL"));
1664         assertNull(StringUtils.defaultIfBlank("", null));
1665         // Tests compatibility for the API return type
1666         final String s = StringUtils.defaultIfBlank("abc", "NULL");
1667         assertEquals("abc", s);
1668     }
1669 
1670     @Test
1671     public void testDefaultIfEmpty_StringBuilders() {
1672         assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
1673         assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
1674         assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), null));
1675         // Tests compatibility for the API return type
1676         final StringBuilder s = StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL"));
1677         assertEquals("abc", s.toString());
1678     }
1679 
1680     @Test
1681     public void testDefaultIfBlank_StringBuilders() {
1682         assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(""), new StringBuilder("NULL")).toString());
1683         assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(" "), new StringBuilder("NULL")).toString());
1684         assertEquals("abc", StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
1685         assertNull(StringUtils.defaultIfBlank(new StringBuilder(""), null));
1686         // Tests compatibility for the API return type
1687         final StringBuilder s = StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL"));
1688         assertEquals("abc", s.toString());
1689     }
1690 
1691     @Test
1692     public void testDefaultIfEmpty_StringBuffers() {
1693         assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
1694         assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
1695         assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), null));
1696         // Tests compatibility for the API return type
1697         final StringBuffer s = StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL"));
1698         assertEquals("abc", s.toString());
1699     }
1700 
1701     @Test
1702     public void testDefaultIfBlank_StringBuffers() {
1703         assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(""), new StringBuffer("NULL")).toString());
1704         assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(" "), new StringBuffer("NULL")).toString());
1705         assertEquals("abc", StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
1706         assertNull(StringUtils.defaultIfBlank(new StringBuffer(""), null));
1707         // Tests compatibility for the API return type
1708         final StringBuffer s = StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL"));
1709         assertEquals("abc", s.toString());
1710     }
1711 
1712     @Test
1713     public void testDefaultIfEmpty_CharBuffers() {
1714         assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
1715         assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
1716         assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), null));
1717         // Tests compatibility for the API return type
1718         final CharBuffer s = StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
1719         assertEquals("abc", s.toString());
1720     }
1721 
1722     @Test
1723     public void testDefaultIfBlank_CharBuffers() {
1724         assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
1725         assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(" "), CharBuffer.wrap("NULL")).toString());
1726         assertEquals("abc", StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
1727         assertNull(StringUtils.defaultIfBlank(CharBuffer.wrap(""), null));
1728         // Tests compatibility for the API return type
1729         final CharBuffer s = StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
1730         assertEquals("abc", s.toString());
1731     }
1732 
1733     //-----------------------------------------------------------------------
1734     @Test
1735     public void testAbbreviate_StringInt() {
1736         assertNull(StringUtils.abbreviate(null, 10));
1737         assertEquals("", StringUtils.abbreviate("", 10));
1738         assertEquals("short", StringUtils.abbreviate("short", 10));
1739         assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
1740 
1741         final String raspberry = "raspberry peach";
1742         assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
1743         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
1744         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
1745         assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
1746         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
1747         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
1748         assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
1749         assertEquals("", StringUtils.abbreviate("", 4));
1750         
1751         try {
1752             @SuppressWarnings("unused")
1753             final
1754             String res = StringUtils.abbreviate("abc", 3);
1755             fail("StringUtils.abbreviate expecting IllegalArgumentException");
1756         } catch (final IllegalArgumentException ex) {
1757                 // empty
1758         }              
1759     }
1760     
1761     @Test
1762     public void testAbbreviate_StringIntInt() {
1763         assertNull(StringUtils.abbreviate(null, 10, 12));
1764         assertEquals("", StringUtils.abbreviate("", 0, 10));
1765         assertEquals("", StringUtils.abbreviate("", 2, 10));
1766         
1767         try {
1768             @SuppressWarnings("unused")
1769             final
1770             String res = StringUtils.abbreviate("abcdefghij", 0, 3);
1771             fail("StringUtils.abbreviate expecting IllegalArgumentException");
1772         } catch (final IllegalArgumentException ex) {
1773                 // empty
1774         }      
1775         try {
1776             @SuppressWarnings("unused")
1777             final
1778             String res = StringUtils.abbreviate("abcdefghij", 5, 6);
1779             fail("StringUtils.abbreviate expecting IllegalArgumentException");
1780         } catch (final IllegalArgumentException ex) {
1781                 // empty
1782         }      
1783         
1784 
1785         final String raspberry = "raspberry peach";
1786         assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
1787 
1788         assertNull(StringUtils.abbreviate(null, 7, 14));
1789         assertAbbreviateWithOffset("abcdefg...", -1, 10);
1790         assertAbbreviateWithOffset("abcdefg...", 0, 10);
1791         assertAbbreviateWithOffset("abcdefg...", 1, 10);
1792         assertAbbreviateWithOffset("abcdefg...", 2, 10);
1793         assertAbbreviateWithOffset("abcdefg...", 3, 10);
1794         assertAbbreviateWithOffset("abcdefg...", 4, 10);
1795         assertAbbreviateWithOffset("...fghi...", 5, 10);
1796         assertAbbreviateWithOffset("...ghij...", 6, 10);
1797         assertAbbreviateWithOffset("...hijk...", 7, 10);
1798         assertAbbreviateWithOffset("...ijklmno", 8, 10);
1799         assertAbbreviateWithOffset("...ijklmno", 9, 10);
1800         assertAbbreviateWithOffset("...ijklmno", 10, 10);
1801         assertAbbreviateWithOffset("...ijklmno", 10, 10);
1802         assertAbbreviateWithOffset("...ijklmno", 11, 10);
1803         assertAbbreviateWithOffset("...ijklmno", 12, 10);
1804         assertAbbreviateWithOffset("...ijklmno", 13, 10);
1805         assertAbbreviateWithOffset("...ijklmno", 14, 10);
1806         assertAbbreviateWithOffset("...ijklmno", 15, 10);
1807         assertAbbreviateWithOffset("...ijklmno", 16, 10);
1808         assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
1809     }
1810 
1811     private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
1812         final String abcdefghijklmno = "abcdefghijklmno";
1813         final String message = "abbreviate(String,int,int) failed";
1814         final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
1815         if (offset >= 0 && offset < abcdefghijklmno.length()) {
1816             assertTrue(message + " -- should contain offset character",
1817                     actual.indexOf((char)('a'+offset)) != -1);
1818         }
1819         assertTrue(message + " -- should not be greater than maxWidth",
1820                 actual.length() <= maxWidth);
1821         assertEquals(message, expected, actual);
1822     }
1823 
1824     @Test
1825     public void testAbbreviateMiddle() {
1826         // javadoc examples
1827         assertNull( StringUtils.abbreviateMiddle(null, null, 0) );
1828         assertEquals( "abc", StringUtils.abbreviateMiddle("abc", null, 0) );
1829         assertEquals( "abc", StringUtils.abbreviateMiddle("abc", ".", 0) );
1830         assertEquals( "abc", StringUtils.abbreviateMiddle("abc", ".", 3) );
1831         assertEquals( "ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4) );
1832 
1833         // JIRA issue (LANG-405) example (slightly different than actual expected result)
1834         assertEquals( 
1835             "A very long text with un...f the text is complete.",
1836             StringUtils.abbreviateMiddle(
1837                 "A very long text with unimportant stuff in the middle but interesting start and " +
1838                 "end to see if the text is complete.", "...", 50) );
1839 
1840         // Test a much longer text :)
1841         final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
1842         assertEquals( 
1843             "Start text->Close text",
1844             StringUtils.abbreviateMiddle( longText, "->", 22 ) );
1845 
1846         // Test negative length
1847         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
1848 
1849         // Test boundaries
1850         // Fails to change anything as method ensures first and last char are kept
1851         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
1852         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
1853 
1854         // Test length of n=1
1855         assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
1856 
1857         // Test smallest length that can lead to success
1858         assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
1859 
1860         // More from LANG-405
1861         assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
1862         assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
1863     }
1864 
1865     //-----------------------------------------------------------------------
1866     @Test
1867     public void testDifference_StringString() {
1868         assertNull(StringUtils.difference(null, null));
1869         assertEquals("", StringUtils.difference("", ""));
1870         assertEquals("abc", StringUtils.difference("", "abc"));
1871         assertEquals("", StringUtils.difference("abc", ""));
1872         assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
1873         assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
1874         assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
1875         assertEquals("", StringUtils.difference("abc", "abc"));
1876         assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
1877     }
1878 
1879     @Test
1880     public void testDifferenceAt_StringString() {
1881         assertEquals(-1, StringUtils.indexOfDifference(null, null));
1882         assertEquals(0, StringUtils.indexOfDifference(null, "i am a robot"));
1883         assertEquals(-1, StringUtils.indexOfDifference("", ""));
1884         assertEquals(0, StringUtils.indexOfDifference("", "abc"));
1885         assertEquals(0, StringUtils.indexOfDifference("abc", ""));
1886         assertEquals(0, StringUtils.indexOfDifference("i am a machine", null));
1887         assertEquals(7, StringUtils.indexOfDifference("i am a machine", "i am a robot"));
1888         assertEquals(-1, StringUtils.indexOfDifference("foo", "foo"));
1889         assertEquals(0, StringUtils.indexOfDifference("i am a robot", "you are a robot"));
1890         //System.out.println("indexOfDiff: " + StringUtils.indexOfDifference("i am a robot", "not machine"));
1891     }
1892 
1893     //-----------------------------------------------------------------------
1894     @Test
1895     public void testGetLevenshteinDistance_StringString() {
1896         assertEquals(0, StringUtils.getLevenshteinDistance("", "") );
1897         assertEquals(1, StringUtils.getLevenshteinDistance("", "a") );
1898         assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "") );
1899         assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog") );
1900         assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant") );
1901         assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo") );
1902         assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
1903         assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") );
1904         assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo") );
1905         assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo") );
1906     }
1907 
1908     @Test(expected = IllegalArgumentException.class)
1909     public void testGetLevenshteinDistance_NullString() throws Exception {
1910         StringUtils.getLevenshteinDistance("a", null);
1911     }
1912 
1913     @Test(expected = IllegalArgumentException.class)
1914     public void testGetLevenshteinDistance_StringNull() throws Exception {
1915         StringUtils.getLevenshteinDistance(null, "a");
1916     }
1917 
1918     @Test
1919     public void testGetLevenshteinDistance_StringStringInt() {
1920         // empty strings
1921         assertEquals(0, StringUtils.getLevenshteinDistance("", "", 0));
1922         assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 8));
1923         assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "", 7));
1924         assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "", 6));
1925 
1926         // unequal strings, zero threshold
1927         assertEquals(-1, StringUtils.getLevenshteinDistance("b", "a", 0));
1928         assertEquals(-1, StringUtils.getLevenshteinDistance("a", "b", 0));
1929     
1930         // equal strings
1931         assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 0));
1932         assertEquals(0, StringUtils.getLevenshteinDistance("aa", "aa", 2));
1933 
1934         // same length
1935         assertEquals(-1, StringUtils.getLevenshteinDistance("aaa", "bbb", 2));
1936         assertEquals(3, StringUtils.getLevenshteinDistance("aaa", "bbb", 3));
1937     
1938         // big stripe
1939         assertEquals(6, StringUtils.getLevenshteinDistance("aaaaaa", "b", 10));
1940 
1941         // distance less than threshold
1942         assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 8));
1943         assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 4));
1944     
1945         // distance equal to threshold
1946         assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "b", 7));
1947         assertEquals(3, StringUtils.getLevenshteinDistance("a", "bbb", 3));
1948 
1949         // distance greater than threshold
1950         assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 2));
1951         assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 2));
1952         assertEquals(-1, StringUtils.getLevenshteinDistance("aaapppp", "b", 6));
1953 
1954         // stripe runs off array, strings not similar
1955         assertEquals(-1, StringUtils.getLevenshteinDistance("a", "bbb", 1));
1956         assertEquals(-1, StringUtils.getLevenshteinDistance("bbb", "a", 1));
1957 
1958         // stripe runs off array, strings are similar
1959         assertEquals(-1, StringUtils.getLevenshteinDistance("12345", "1234567", 1));
1960         assertEquals(-1, StringUtils.getLevenshteinDistance("1234567", "12345", 1));
1961 
1962         // old getLevenshteinDistance test cases
1963         assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog",1) );
1964         assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant",3) );
1965         assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo",7) );
1966         assertEquals(-1, StringUtils.getLevenshteinDistance("elephant", "hippo",6) );
1967         assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant",7) );
1968         assertEquals(-1, StringUtils.getLevenshteinDistance("hippo", "elephant",6) );
1969         assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz",8) );
1970         assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo",8) );
1971         assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo",1) );
1972 
1973         assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", Integer.MAX_VALUE) );
1974         assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", Integer.MAX_VALUE) );
1975         assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", Integer.MAX_VALUE) );
1976         assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", Integer.MAX_VALUE) );
1977         assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", Integer.MAX_VALUE) );
1978         assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", Integer.MAX_VALUE) );
1979         assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", Integer.MAX_VALUE));
1980     }
1981 
1982     @Test(expected = IllegalArgumentException.class)
1983     public void testGetLevenshteinDistance_NullStringInt() throws Exception {
1984             StringUtils.getLevenshteinDistance(null, "a", 0);
1985     }
1986 
1987     @Test(expected = IllegalArgumentException.class)
1988     public void testGetLevenshteinDistance_StringNullInt() throws Exception {
1989             StringUtils.getLevenshteinDistance("a", null, 0);
1990     }
1991 
1992     @Test(expected = IllegalArgumentException.class)
1993     public void testGetLevenshteinDistance_StringStringNegativeInt() throws Exception {
1994             StringUtils.getLevenshteinDistance("a", "a", -1);
1995     }
1996 
1997     @Test
1998     public void testGetJaroWinklerDistance_StringString() {
1999         assertEquals(0.93d, StringUtils.getJaroWinklerDistance("frog", "fog"), 0.0d);
2000         assertEquals(0.0d, StringUtils.getJaroWinklerDistance("fly", "ant"), 0.0d);
2001         assertEquals(0.44d, StringUtils.getJaroWinklerDistance("elephant", "hippo"), 0.0d);
2002         assertEquals(0.91d, StringUtils.getJaroWinklerDistance("ABC Corporation", "ABC Corp"), 0.0d);
2003         assertEquals(0.93d, StringUtils.getJaroWinklerDistance("D N H Enterprises Inc", "D & H Enterprises, Inc."), 0.0d);
2004         assertEquals(0.94d, StringUtils.getJaroWinklerDistance("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"), 0.0d);
2005         assertEquals(0.9d, StringUtils.getJaroWinklerDistance("PENNSYLVANIA", "PENNCISYLVNIA"), 0.0d);
2006     }
2007 
2008     @Test(expected = IllegalArgumentException.class)
2009     public void testGetJaroWinklerDistance_NullNull() throws Exception {
2010             StringUtils.getJaroWinklerDistance(null, null);
2011     }
2012 
2013     @Test(expected = IllegalArgumentException.class)
2014     public void testGetJaroWinklerDistance_StringNull() throws Exception {
2015             StringUtils.getJaroWinklerDistance(" ", null);
2016     }
2017 
2018     @Test(expected = IllegalArgumentException.class)
2019     public void testGetJaroWinklerDistance_NullString() throws Exception {
2020             StringUtils.getJaroWinklerDistance(null, "clear");
2021     }
2022 
2023     @Test
2024     public void testGetFuzzyDistance() throws Exception {
2025         assertEquals(0, StringUtils.getFuzzyDistance("", "", Locale.ENGLISH));
2026         assertEquals(0, StringUtils.getFuzzyDistance("Workshop", "b", Locale.ENGLISH));
2027         assertEquals(1, StringUtils.getFuzzyDistance("Room", "o", Locale.ENGLISH));
2028         assertEquals(1, StringUtils.getFuzzyDistance("Workshop", "w", Locale.ENGLISH));
2029         assertEquals(2, StringUtils.getFuzzyDistance("Workshop", "ws", Locale.ENGLISH));
2030         assertEquals(4, StringUtils.getFuzzyDistance("Workshop", "wo", Locale.ENGLISH));
2031         assertEquals(3, StringUtils.getFuzzyDistance("Apache Software Foundation", "asf", Locale.ENGLISH));
2032     }
2033 
2034     @Test(expected = IllegalArgumentException.class)
2035     public void testGetFuzzyDistance_NullNullNull() throws Exception {
2036         StringUtils.getFuzzyDistance(null, null, null);
2037     }
2038 
2039     @Test(expected = IllegalArgumentException.class)
2040     public void testGetFuzzyDistance_StringNullLoclae() throws Exception {
2041         StringUtils.getFuzzyDistance(" ", null, Locale.ENGLISH);
2042     }
2043 
2044     @Test(expected = IllegalArgumentException.class)
2045     public void testGetFuzzyDistance_NullStringLocale() throws Exception {
2046         StringUtils.getFuzzyDistance(null, "clear", Locale.ENGLISH);
2047     }
2048 
2049     @Test(expected = IllegalArgumentException.class)
2050     public void testGetFuzzyDistance_StringStringNull() throws Exception {
2051         StringUtils.getFuzzyDistance(" ", "clear", null);
2052     }
2053 
2054     /**
2055      * A sanity check for {@link StringUtils#EMPTY}.
2056      */
2057     @Test
2058     public void testEMPTY() {
2059         assertNotNull(StringUtils.EMPTY);
2060         assertEquals("", StringUtils.EMPTY);
2061         assertEquals(0, StringUtils.EMPTY.length());
2062     }
2063 
2064     /**
2065      * Test for {@link StringUtils#isAllLowerCase(CharSequence)}.
2066      */
2067     @Test
2068     public void testIsAllLowerCase() {
2069         assertFalse(StringUtils.isAllLowerCase(null));
2070         assertFalse(StringUtils.isAllLowerCase(StringUtils.EMPTY));
2071         assertFalse(StringUtils.isAllLowerCase("  "));
2072         assertTrue(StringUtils.isAllLowerCase("abc"));
2073         assertFalse(StringUtils.isAllLowerCase("abc "));
2074         assertFalse(StringUtils.isAllLowerCase("abc\n"));
2075         assertFalse(StringUtils.isAllLowerCase("abC"));
2076         assertFalse(StringUtils.isAllLowerCase("ab c"));
2077         assertFalse(StringUtils.isAllLowerCase("ab1c"));
2078         assertFalse(StringUtils.isAllLowerCase("ab/c"));
2079     }
2080 
2081     /**
2082      * Test for {@link StringUtils#isAllUpperCase(CharSequence)}.
2083      */
2084     @Test
2085     public void testIsAllUpperCase() {
2086         assertFalse(StringUtils.isAllUpperCase(null));
2087         assertFalse(StringUtils.isAllUpperCase(StringUtils.EMPTY));
2088         assertFalse(StringUtils.isAllUpperCase("  "));
2089         assertTrue(StringUtils.isAllUpperCase("ABC"));
2090         assertFalse(StringUtils.isAllUpperCase("ABC "));
2091         assertFalse(StringUtils.isAllUpperCase("ABC\n"));
2092         assertFalse(StringUtils.isAllUpperCase("aBC"));
2093         assertFalse(StringUtils.isAllUpperCase("A C"));
2094         assertFalse(StringUtils.isAllUpperCase("A1C"));
2095         assertFalse(StringUtils.isAllUpperCase("A/C"));
2096     }
2097 
2098     @Test
2099     public void testRemoveStart() {
2100         // StringUtils.removeStart("", *)        = ""
2101         assertNull(StringUtils.removeStart(null, null));
2102         assertNull(StringUtils.removeStart(null, ""));
2103         assertNull(StringUtils.removeStart(null, "a"));
2104         
2105         // StringUtils.removeStart(*, null)      = *
2106         assertEquals(StringUtils.removeStart("", null), "");
2107         assertEquals(StringUtils.removeStart("", ""), "");
2108         assertEquals(StringUtils.removeStart("", "a"), "");
2109         
2110         // All others:
2111         assertEquals(StringUtils.removeStart("www.domain.com", "www."), "domain.com");
2112         assertEquals(StringUtils.removeStart("domain.com", "www."), "domain.com");
2113         assertEquals(StringUtils.removeStart("domain.com", ""), "domain.com");        
2114         assertEquals(StringUtils.removeStart("domain.com", null), "domain.com");        
2115     }
2116     
2117     @Test
2118     public void testRemoveStartIgnoreCase() {
2119         // StringUtils.removeStart("", *)        = ""
2120         assertNull("removeStartIgnoreCase(null, null)", StringUtils.removeStartIgnoreCase(null, null));
2121         assertNull("removeStartIgnoreCase(null, \"\")", StringUtils.removeStartIgnoreCase(null, ""));
2122         assertNull("removeStartIgnoreCase(null, \"a\")", StringUtils.removeStartIgnoreCase(null, "a"));
2123         
2124         // StringUtils.removeStart(*, null)      = *
2125         assertEquals("removeStartIgnoreCase(\"\", null)", StringUtils.removeStartIgnoreCase("", null), "");
2126         assertEquals("removeStartIgnoreCase(\"\", \"\")", StringUtils.removeStartIgnoreCase("", ""), "");
2127         assertEquals("removeStartIgnoreCase(\"\", \"a\")", StringUtils.removeStartIgnoreCase("", "a"), "");
2128         
2129         // All others:
2130         assertEquals("removeStartIgnoreCase(\"www.domain.com\", \"www.\")", StringUtils.removeStartIgnoreCase("www.domain.com", "www."), "domain.com");
2131         assertEquals("removeStartIgnoreCase(\"domain.com\", \"www.\")", StringUtils.removeStartIgnoreCase("domain.com", "www."), "domain.com");
2132         assertEquals("removeStartIgnoreCase(\"domain.com\", \"\")", StringUtils.removeStartIgnoreCase("domain.com", ""), "domain.com");        
2133         assertEquals("removeStartIgnoreCase(\"domain.com\", null)", StringUtils.removeStartIgnoreCase("domain.com", null), "domain.com");        
2134         
2135         // Case insensitive:
2136         assertEquals("removeStartIgnoreCase(\"www.domain.com\", \"WWW.\")", StringUtils.removeStartIgnoreCase("www.domain.com", "WWW."), "domain.com");
2137     }
2138 
2139     @Test
2140     public void testRemoveEnd() {
2141         // StringUtils.removeEnd("", *)        = ""
2142         assertNull(StringUtils.removeEnd(null, null));
2143         assertNull(StringUtils.removeEnd(null, ""));
2144         assertNull(StringUtils.removeEnd(null, "a"));
2145         
2146         // StringUtils.removeEnd(*, null)      = *
2147         assertEquals(StringUtils.removeEnd("", null), "");
2148         assertEquals(StringUtils.removeEnd("", ""), "");
2149         assertEquals(StringUtils.removeEnd("", "a"), "");
2150         
2151         // All others:
2152         assertEquals(StringUtils.removeEnd("www.domain.com.", ".com"), "www.domain.com.");
2153         assertEquals(StringUtils.removeEnd("www.domain.com", ".com"), "www.domain");
2154         assertEquals(StringUtils.removeEnd("www.domain", ".com"), "www.domain");
2155         assertEquals(StringUtils.removeEnd("domain.com", ""), "domain.com");   
2156         assertEquals(StringUtils.removeEnd("domain.com", null), "domain.com");   
2157     }
2158 
2159     @Test
2160     public void testRemoveEndIgnoreCase() {
2161         // StringUtils.removeEndIgnoreCase("", *)        = ""
2162         assertNull("removeEndIgnoreCase(null, null)", StringUtils.removeEndIgnoreCase(null, null));
2163         assertNull("removeEndIgnoreCase(null, \"\")", StringUtils.removeEndIgnoreCase(null, ""));
2164         assertNull("removeEndIgnoreCase(null, \"a\")", StringUtils.removeEndIgnoreCase(null, "a"));
2165         
2166         // StringUtils.removeEnd(*, null)      = *
2167         assertEquals("removeEndIgnoreCase(\"\", null)", StringUtils.removeEndIgnoreCase("", null), "");
2168         assertEquals("removeEndIgnoreCase(\"\", \"\")", StringUtils.removeEndIgnoreCase("", ""), "");
2169         assertEquals("removeEndIgnoreCase(\"\", \"a\")", StringUtils.removeEndIgnoreCase("", "a"), "");
2170         
2171         // All others:
2172         assertEquals("removeEndIgnoreCase(\"www.domain.com.\", \".com\")", StringUtils.removeEndIgnoreCase("www.domain.com.", ".com"), "www.domain.com.");
2173         assertEquals("removeEndIgnoreCase(\"www.domain.com\", \".com\")", StringUtils.removeEndIgnoreCase("www.domain.com", ".com"), "www.domain");
2174         assertEquals("removeEndIgnoreCase(\"www.domain\", \".com\")", StringUtils.removeEndIgnoreCase("www.domain", ".com"), "www.domain");
2175         assertEquals("removeEndIgnoreCase(\"domain.com\", \"\")", StringUtils.removeEndIgnoreCase("domain.com", ""), "domain.com");   
2176         assertEquals("removeEndIgnoreCase(\"domain.com\", null)", StringUtils.removeEndIgnoreCase("domain.com", null), "domain.com");   
2177 
2178         // Case insensitive:
2179         assertEquals("removeEndIgnoreCase(\"www.domain.com\", \".COM\")", StringUtils.removeEndIgnoreCase("www.domain.com", ".COM"), "www.domain");
2180         assertEquals("removeEndIgnoreCase(\"www.domain.COM\", \".com\")", StringUtils.removeEndIgnoreCase("www.domain.COM", ".com"), "www.domain");
2181     }
2182 
2183     @Test
2184     public void testRemove_String() {
2185         // StringUtils.remove(null, *)        = null
2186         assertNull(StringUtils.remove(null, null));
2187         assertNull(StringUtils.remove(null, ""));
2188         assertNull(StringUtils.remove(null, "a"));
2189         
2190         // StringUtils.remove("", *)          = ""
2191         assertEquals("", StringUtils.remove("", null));
2192         assertEquals("", StringUtils.remove("", ""));
2193         assertEquals("", StringUtils.remove("", "a"));
2194         
2195         // StringUtils.remove(*, null)        = *
2196         assertNull(StringUtils.remove(null, null));
2197         assertEquals("", StringUtils.remove("", null));
2198         assertEquals("a", StringUtils.remove("a", null));
2199         
2200         // StringUtils.remove(*, "")          = *
2201         assertNull(StringUtils.remove(null, ""));
2202         assertEquals("", StringUtils.remove("", ""));
2203         assertEquals("a", StringUtils.remove("a", ""));
2204         
2205         // StringUtils.remove("queued", "ue") = "qd"
2206         assertEquals("qd", StringUtils.remove("queued", "ue"));
2207         
2208         // StringUtils.remove("queued", "zz") = "queued"
2209         assertEquals("queued", StringUtils.remove("queued", "zz"));
2210     }
2211 
2212     @Test
2213     public void testRemove_char() {
2214         // StringUtils.remove(null, *)       = null
2215         assertNull(StringUtils.remove(null, 'a'));
2216         assertNull(StringUtils.remove(null, 'a'));
2217         assertNull(StringUtils.remove(null, 'a'));
2218         
2219         // StringUtils.remove("", *)          = ""
2220         assertEquals("", StringUtils.remove("", 'a'));
2221         assertEquals("", StringUtils.remove("", 'a'));
2222         assertEquals("", StringUtils.remove("", 'a'));
2223         
2224         // StringUtils.remove("queued", 'u') = "qeed"
2225         assertEquals("qeed", StringUtils.remove("queued", 'u'));
2226         
2227         // StringUtils.remove("queued", 'z') = "queued"
2228         assertEquals("queued", StringUtils.remove("queued", 'z'));
2229     }
2230     
2231     @Test
2232     public void testDifferenceAt_StringArray() {        
2233         assertEquals(-1, StringUtils.indexOfDifference((String[])null));
2234         assertEquals(-1, StringUtils.indexOfDifference(new String[] {}));
2235         assertEquals(-1, StringUtils.indexOfDifference(new String[] {"abc"}));
2236         assertEquals(-1, StringUtils.indexOfDifference(new String[] {null, null}));
2237         assertEquals(-1, StringUtils.indexOfDifference(new String[] {"", ""}));
2238         assertEquals(0, StringUtils.indexOfDifference(new String[] {"", null}));
2239         assertEquals(0, StringUtils.indexOfDifference(new String[] {"abc", null, null}));
2240         assertEquals(0, StringUtils.indexOfDifference(new String[] {null, null, "abc"}));
2241         assertEquals(0, StringUtils.indexOfDifference(new String[] {"", "abc"}));
2242         assertEquals(0, StringUtils.indexOfDifference(new String[] {"abc", ""}));
2243         assertEquals(-1, StringUtils.indexOfDifference(new String[] {"abc", "abc"}));
2244         assertEquals(1, StringUtils.indexOfDifference(new String[] {"abc", "a"}));
2245         assertEquals(2, StringUtils.indexOfDifference(new String[] {"ab", "abxyz"}));
2246         assertEquals(2, StringUtils.indexOfDifference(new String[] {"abcde", "abxyz"}));
2247         assertEquals(0, StringUtils.indexOfDifference(new String[] {"abcde", "xyz"}));
2248         assertEquals(0, StringUtils.indexOfDifference(new String[] {"xyz", "abcde"}));
2249         assertEquals(7, StringUtils.indexOfDifference(new String[] {"i am a machine", "i am a robot"}));
2250     }
2251     
2252     @Test
2253     public void testGetCommonPrefix_StringArray() {
2254         assertEquals("", StringUtils.getCommonPrefix((String[])null));
2255         assertEquals("", StringUtils.getCommonPrefix());
2256         assertEquals("abc", StringUtils.getCommonPrefix("abc"));
2257         assertEquals("", StringUtils.getCommonPrefix(null, null));
2258         assertEquals("", StringUtils.getCommonPrefix("", ""));
2259         assertEquals("", StringUtils.getCommonPrefix("", null));
2260         assertEquals("", StringUtils.getCommonPrefix("abc", null, null));
2261         assertEquals("", StringUtils.getCommonPrefix(null, null, "abc"));
2262         assertEquals("", StringUtils.getCommonPrefix("", "abc"));
2263         assertEquals("", StringUtils.getCommonPrefix("abc", ""));
2264         assertEquals("abc", StringUtils.getCommonPrefix("abc", "abc"));
2265         assertEquals("a", StringUtils.getCommonPrefix("abc", "a"));
2266         assertEquals("ab", StringUtils.getCommonPrefix("ab", "abxyz"));
2267         assertEquals("ab", StringUtils.getCommonPrefix("abcde", "abxyz"));
2268         assertEquals("", StringUtils.getCommonPrefix("abcde", "xyz"));
2269         assertEquals("", StringUtils.getCommonPrefix("xyz", "abcde"));
2270         assertEquals("i am a ", StringUtils.getCommonPrefix("i am a machine", "i am a robot"));
2271     }
2272 
2273     @Test
2274     public void testNormalizeSpace() {
2275         assertNull(StringUtils.normalizeSpace(null));
2276         assertEquals("", StringUtils.normalizeSpace(""));
2277         assertEquals("", StringUtils.normalizeSpace(" "));
2278         assertEquals("", StringUtils.normalizeSpace("\t"));
2279         assertEquals("", StringUtils.normalizeSpace("\n"));
2280         assertEquals("", StringUtils.normalizeSpace("\u0009"));
2281         assertEquals("", StringUtils.normalizeSpace("\u000B"));
2282         assertEquals("", StringUtils.normalizeSpace("\u000C"));
2283         assertEquals("", StringUtils.normalizeSpace("\u001C"));
2284         assertEquals("", StringUtils.normalizeSpace("\u001D"));
2285         assertEquals("", StringUtils.normalizeSpace("\u001E"));
2286         assertEquals("", StringUtils.normalizeSpace("\u001F"));
2287         assertEquals("", StringUtils.normalizeSpace("\f"));
2288         assertEquals("", StringUtils.normalizeSpace("\r"));
2289         assertEquals("a", StringUtils.normalizeSpace("  a  "));
2290         assertEquals("a b c", StringUtils.normalizeSpace("  a  b   c  "));
2291         assertEquals("a b c", StringUtils.normalizeSpace("a\t\f\r  b\u000B   c\n"));
2292         assertEquals("a   b c", StringUtils.normalizeSpace("a\t\f\r  " + HARD_SPACE + HARD_SPACE + "b\u000B   c\n"));
2293     }
2294 
2295     @Test
2296     public void testLANG666() {
2297         assertEquals("12",StringUtils.stripEnd("120.00", ".0"));
2298         assertEquals("121",StringUtils.stripEnd("121.00", ".0"));
2299     }
2300 
2301     // Methods on StringUtils that are immutable in spirit (i.e. calculate the length) 
2302     // should take a CharSequence parameter. Methods that are mutable in spirit (i.e. capitalize) 
2303     // should take a String or String[] parameter and return String or String[].
2304     // This test enforces that this is done.
2305     @Test
2306     public void testStringUtilsCharSequenceContract() {
2307         final Class<StringUtils> c = StringUtils.class;
2308         final Method[] methods = c.getMethods();
2309         for (final Method m : methods) {
2310             if (m.getReturnType() == String.class || m.getReturnType() == String[].class) {
2311                 // Assume this is mutable and ensure the first parameter is not CharSequence.
2312                 // It may be String or it may be something else (String[], Object, Object[]) so 
2313                 // don't actively test for that.
2314                 final Class<?>[] params = m.getParameterTypes();
2315                 if ( params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
2316                     fail("The method " + m + " appears to be mutable in spirit and therefore must not accept a CharSequence");
2317                 }
2318             } else {
2319                 // Assume this is immutable in spirit and ensure the first parameter is not String.
2320                 // As above, it may be something other than CharSequence.
2321                 final Class<?>[] params = m.getParameterTypes();
2322                 if ( params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
2323                     fail("The method " + m + " appears to be immutable in spirit and therefore must not accept a String");
2324                 }
2325             }
2326         }
2327     }
2328 
2329     /**
2330      * Tests {@link StringUtils#toString(byte[], String)}
2331      *
2332      * @throws java.io.UnsupportedEncodingException because the method under test max throw it
2333      * @see StringUtils#toString(byte[], String)
2334      */
2335     @Test
2336     public void testToString() throws UnsupportedEncodingException {
2337         final String expectedString = "The quick brown fox jumped over the lazy dog.";
2338         byte[] expectedBytes = expectedString.getBytes(Charset.defaultCharset());
2339         // sanity check start
2340         assertArrayEquals(expectedBytes, expectedString.getBytes());
2341         // sanity check end
2342         assertEquals(expectedString, StringUtils.toString(expectedBytes, null));
2343         assertEquals(expectedString, StringUtils.toString(expectedBytes, SystemUtils.FILE_ENCODING));
2344         String encoding = "UTF-16";
2345         expectedBytes = expectedString.getBytes(Charset.forName(encoding));
2346         assertEquals(expectedString, StringUtils.toString(expectedBytes, encoding));
2347     }
2348     
2349     @Test
2350     public void testEscapeSurrogatePairs() throws Exception {
2351         assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
2352         // Examples from https://en.wikipedia.org/wiki/UTF-16
2353         assertEquals("\uD800\uDC00", StringEscapeUtils.escapeCsv("\uD800\uDC00"));
2354         assertEquals("\uD834\uDD1E", StringEscapeUtils.escapeCsv("\uD834\uDD1E"));
2355         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeCsv("\uDBFF\uDFFD"));
2356         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml3("\uDBFF\uDFFD"));
2357         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml4("\uDBFF\uDFFD"));
2358         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeXml("\uDBFF\uDFFD"));
2359     }
2360     
2361     /**
2362      * Tests LANG-858.
2363      */
2364     @Test
2365     public void testEscapeSurrogatePairsLang858() {
2366         assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeJava("\uDBFF\uDFFD"));       //fail LANG-858
2367         assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD")); //fail LANG-858
2368     }
2369     
2370     @Test
2371     public void testUnescapeSurrogatePairs() throws Exception {
2372         assertEquals("\uD83D\uDE30", StringEscapeUtils.unescapeCsv("\uD83D\uDE30"));
2373         // Examples from https://en.wikipedia.org/wiki/UTF-16
2374         assertEquals("\uD800\uDC00", StringEscapeUtils.unescapeCsv("\uD800\uDC00"));
2375         assertEquals("\uD834\uDD1E", StringEscapeUtils.unescapeCsv("\uD834\uDD1E"));
2376         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeCsv("\uDBFF\uDFFD"));
2377         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeHtml3("\uDBFF\uDFFD"));
2378         assertEquals("\uDBFF\uDFFD", StringEscapeUtils.unescapeHtml4("\uDBFF\uDFFD"));
2379     }
2380 
2381     /**
2382      * Tests {@code appendIfMissing}.
2383      */
2384     @Test
2385     public void testAppendIfMissing() {
2386         assertEquals("appendIfMissing(null,null)", null, StringUtils.appendIfMissing(null,null));
2387         assertEquals("appendIfMissing(abc,null)", "abc", StringUtils.appendIfMissing("abc",null));
2388         assertEquals("appendIfMissing(\"\",xyz)", "xyz", StringUtils.appendIfMissing("","xyz"));
2389         assertEquals("appendIfMissing(abc,xyz)", "abcxyz", StringUtils.appendIfMissing("abc","xyz"));
2390         assertEquals("appendIfMissing(abcxyz,xyz)", "abcxyz", StringUtils.appendIfMissing("abcxyz","xyz"));
2391         assertEquals("appendIfMissing(aXYZ,xyz)", "aXYZxyz", StringUtils.appendIfMissing("aXYZ","xyz"));
2392 
2393         assertEquals("appendIfMissing(null,null,null)", null, StringUtils.appendIfMissing(null,null,(CharSequence[]) null));
2394         assertEquals("appendIfMissing(abc,null,null)", "abc", StringUtils.appendIfMissing("abc",null,(CharSequence[]) null));
2395         assertEquals("appendIfMissing(\"\",xyz,null))", "xyz", StringUtils.appendIfMissing("","xyz",(CharSequence[]) null));
2396         assertEquals("appendIfMissing(abc,xyz,{null})", "abcxyz", StringUtils.appendIfMissing("abc","xyz",new CharSequence[]{null}));
2397         assertEquals("appendIfMissing(abc,xyz,\"\")", "abc", StringUtils.appendIfMissing("abc","xyz",""));
2398         assertEquals("appendIfMissing(abc,xyz,mno)", "abcxyz", StringUtils.appendIfMissing("abc","xyz","mno"));
2399         assertEquals("appendIfMissing(abcxyz,xyz,mno)", "abcxyz", StringUtils.appendIfMissing("abcxyz","xyz","mno"));
2400         assertEquals("appendIfMissing(abcmno,xyz,mno)", "abcmno", StringUtils.appendIfMissing("abcmno","xyz","mno"));
2401         assertEquals("appendIfMissing(abcXYZ,xyz,mno)", "abcXYZxyz", StringUtils.appendIfMissing("abcXYZ","xyz","mno"));
2402         assertEquals("appendIfMissing(abcMNO,xyz,mno)", "abcMNOxyz", StringUtils.appendIfMissing("abcMNO","xyz","mno"));
2403     }
2404 
2405     /**
2406      * Tests {@code appendIfMissingIgnoreCase}.
2407      */
2408     @Test
2409     public void testAppendIfMissingIgnoreCase() {
2410         assertEquals("appendIfMissingIgnoreCase(null,null)", null, StringUtils.appendIfMissingIgnoreCase(null,null));
2411         assertEquals("appendIfMissingIgnoreCase(abc,null)", "abc", StringUtils.appendIfMissingIgnoreCase("abc",null));
2412         assertEquals("appendIfMissingIgnoreCase(\"\",xyz)", "xyz", StringUtils.appendIfMissingIgnoreCase("","xyz"));
2413         assertEquals("appendIfMissingIgnoreCase(abc,xyz)", "abcxyz", StringUtils.appendIfMissingIgnoreCase("abc","xyz"));
2414         assertEquals("appendIfMissingIgnoreCase(abcxyz,xyz)", "abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz","xyz"));
2415         assertEquals("appendIfMissingIgnoreCase(abcXYZ,xyz)", "abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz"));
2416 
2417         assertEquals("appendIfMissingIgnoreCase(null,null,null)", null, StringUtils.appendIfMissingIgnoreCase(null,null,(CharSequence[]) null));
2418         assertEquals("appendIfMissingIgnoreCase(abc,null,null)", "abc", StringUtils.appendIfMissingIgnoreCase("abc",null,(CharSequence[]) null));
2419         assertEquals("appendIfMissingIgnoreCase(\"\",xyz,null)", "xyz", StringUtils.appendIfMissingIgnoreCase("","xyz",(CharSequence[]) null));
2420         assertEquals("appendIfMissingIgnoreCase(abc,xyz,{null})", "abcxyz", StringUtils.appendIfMissingIgnoreCase("abc","xyz",new CharSequence[]{null}));
2421         assertEquals("appendIfMissingIgnoreCase(abc,xyz,\"\")", "abc", StringUtils.appendIfMissingIgnoreCase("abc","xyz",""));
2422         assertEquals("appendIfMissingIgnoreCase(abc,xyz,mno)", "abcxyz", StringUtils.appendIfMissingIgnoreCase("abc","xyz","mno"));
2423         assertEquals("appendIfMissingIgnoreCase(abcxyz,xyz,mno)", "abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz","xyz","mno"));       
2424         assertEquals("appendIfMissingIgnoreCase(abcmno,xyz,mno)", "abcmno", StringUtils.appendIfMissingIgnoreCase("abcmno","xyz","mno"));       
2425         assertEquals("appendIfMissingIgnoreCase(abcXYZ,xyz,mno)", "abcXYZ", StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz","mno"));
2426         assertEquals("appendIfMissingIgnoreCase(abcMNO,xyz,mno)", "abcMNO", StringUtils.appendIfMissingIgnoreCase("abcMNO","xyz","mno"));
2427     }
2428 
2429     /**
2430      * Tests {@code prependIfMissing}.
2431      */
2432     @Test
2433     public void testPrependIfMissing() {
2434         assertEquals("prependIfMissing(null,null)", null, StringUtils.prependIfMissing(null,null));
2435         assertEquals("prependIfMissing(abc,null)", "abc", StringUtils.prependIfMissing("abc",null));
2436         assertEquals("prependIfMissing(\"\",xyz)", "xyz", StringUtils.prependIfMissing("","xyz"));
2437         assertEquals("prependIfMissing(abc,xyz)", "xyzabc", StringUtils.prependIfMissing("abc","xyz"));
2438         assertEquals("prependIfMissing(xyzabc,xyz)", "xyzabc", StringUtils.prependIfMissing("xyzabc","xyz"));
2439         assertEquals("prependIfMissing(XYZabc,xyz)", "xyzXYZabc", StringUtils.prependIfMissing("XYZabc","xyz"));
2440 
2441         assertEquals("prependIfMissing(null,null null)", null, StringUtils.prependIfMissing(null,null,(CharSequence[]) null));
2442         assertEquals("prependIfMissing(abc,null,null)", "abc", StringUtils.prependIfMissing("abc",null,(CharSequence[]) null));
2443         assertEquals("prependIfMissing(\"\",xyz,null)", "xyz", StringUtils.prependIfMissing("","xyz",(CharSequence[]) null));
2444         assertEquals("prependIfMissing(abc,xyz,{null})","xyzabc", StringUtils.prependIfMissing("abc","xyz",new CharSequence[]{null}));
2445         assertEquals("prependIfMissing(abc,xyz,\"\")","abc", StringUtils.prependIfMissing("abc","xyz",""));
2446         assertEquals("prependIfMissing(abc,xyz,mno)","xyzabc", StringUtils.prependIfMissing("abc","xyz","mno"));
2447         assertEquals("prependIfMissing(xyzabc,xyz,mno)", "xyzabc", StringUtils.prependIfMissing("xyzabc","xyz","mno"));
2448         assertEquals("prependIfMissing(mnoabc,xyz,mno)", "mnoabc", StringUtils.prependIfMissing("mnoabc","xyz","mno"));
2449         assertEquals("prependIfMissing(XYZabc,xyz,mno)", "xyzXYZabc", StringUtils.prependIfMissing("XYZabc","xyz","mno"));
2450         assertEquals("prependIfMissing(MNOabc,xyz,mno)", "xyzMNOabc", StringUtils.prependIfMissing("MNOabc","xyz","mno"));
2451     }
2452 
2453     /**
2454      * Tests {@code prependIfMissingIgnoreCase}.
2455      */
2456     @Test
2457     public void testPrependIfMissingIgnoreCase() {
2458         assertEquals("prependIfMissingIgnoreCase(null,null)", null, StringUtils.prependIfMissingIgnoreCase(null,null));
2459         assertEquals("prependIfMissingIgnoreCase(abc,null)", "abc", StringUtils.prependIfMissingIgnoreCase("abc",null));
2460         assertEquals("prependIfMissingIgnoreCase(\"\",xyz)", "xyz", StringUtils.prependIfMissingIgnoreCase("","xyz"));
2461         assertEquals("prependIfMissingIgnoreCase(abc,xyz)", "xyzabc", StringUtils.prependIfMissingIgnoreCase("abc","xyz"));
2462         assertEquals("prependIfMissingIgnoreCase(xyzabc,xyz)", "xyzabc", StringUtils.prependIfMissingIgnoreCase("xyzabc","xyz"));
2463         assertEquals("prependIfMissingIgnoreCase(XYZabc,xyz)", "XYZabc", StringUtils.prependIfMissingIgnoreCase("XYZabc","xyz"));
2464 
2465         assertEquals("prependIfMissingIgnoreCase(null,null null)", null, StringUtils.prependIfMissingIgnoreCase(null,null,(CharSequence[]) null));
2466         assertEquals("prependIfMissingIgnoreCase(abc,null,null)", "abc", StringUtils.prependIfMissingIgnoreCase("abc",null,(CharSequence[]) null));
2467         assertEquals("prependIfMissingIgnoreCase(\"\",xyz,null)", "xyz", StringUtils.prependIfMissingIgnoreCase("","xyz",(CharSequence[]) null));
2468         assertEquals("prependIfMissingIgnoreCase(abc,xyz,{null})","xyzabc", StringUtils.prependIfMissingIgnoreCase("abc","xyz",new CharSequence[]{null}));
2469         assertEquals("prependIfMissingIgnoreCase(abc,xyz,\"\")","abc", StringUtils.prependIfMissingIgnoreCase("abc","xyz",""));
2470         assertEquals("prependIfMissingIgnoreCase(abc,xyz,mno)","xyzabc", StringUtils.prependIfMissingIgnoreCase("abc","xyz","mno"));
2471         assertEquals("prependIfMissingIgnoreCase(xyzabc,xyz,mno)", "xyzabc", StringUtils.prependIfMissingIgnoreCase("xyzabc","xyz","mno"));
2472         assertEquals("prependIfMissingIgnoreCase(mnoabc,xyz,mno)", "mnoabc", StringUtils.prependIfMissingIgnoreCase("mnoabc","xyz","mno"));
2473         assertEquals("prependIfMissingIgnoreCase(XYZabc,xyz,mno)", "XYZabc", StringUtils.prependIfMissingIgnoreCase("XYZabc","xyz","mno"));
2474         assertEquals("prependIfMissingIgnoreCase(MNOabc,xyz,mno)", "MNOabc", StringUtils.prependIfMissingIgnoreCase("MNOabc","xyz","mno"));
2475     }
2476     
2477     /**
2478      * Tests {@link StringUtils#toEncodedString(byte[], Charset)}
2479      * 
2480      * @see StringUtils#toEncodedString(byte[], Charset)
2481      */
2482     @Test
2483     public void testToEncodedString() {
2484         final String expectedString = "The quick brown fox jumped over the lazy dog.";
2485         String encoding = SystemUtils.FILE_ENCODING;
2486         byte[] expectedBytes = expectedString.getBytes(Charset.defaultCharset());
2487         // sanity check start
2488         assertArrayEquals(expectedBytes, expectedString.getBytes());
2489         // sanity check end
2490         assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.defaultCharset()));
2491         assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
2492         encoding = "UTF-16";
2493         expectedBytes = expectedString.getBytes(Charset.forName(encoding));
2494         assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
2495     }
2496     
2497     // -----------------------------------------------------------------------
2498 
2499     @Test
2500     public void testWrap_StringChar() {
2501         assertNull(StringUtils.wrap(null, null));
2502         assertNull(StringUtils.wrap(null, '\0'));
2503         assertNull(StringUtils.wrap(null, '1'));
2504 
2505         assertEquals(null, StringUtils.wrap(null, null));
2506         assertEquals("", StringUtils.wrap("", '\0'));
2507         assertEquals("xabx", StringUtils.wrap("ab", 'x'));
2508         assertEquals("\"ab\"", StringUtils.wrap("ab", '\"'));
2509         assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", '\"'));
2510         assertEquals("'ab'", StringUtils.wrap("ab", '\''));
2511         assertEquals("''abcd''", StringUtils.wrap("'abcd'", '\''));
2512         assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", '\''));
2513         assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", '\"'));
2514     }
2515 
2516     @Test
2517     public void testWrap_StringString() {
2518         assertNull(StringUtils.wrap(null, null));
2519         assertNull(StringUtils.wrap(null, ""));
2520         assertNull(StringUtils.wrap(null, "1"));
2521 
2522         assertEquals(null, StringUtils.wrap(null, null));
2523         assertEquals("", StringUtils.wrap("", ""));
2524         assertEquals("ab", StringUtils.wrap("ab", null));
2525         assertEquals("xabx", StringUtils.wrap("ab", "x"));
2526         assertEquals("\"ab\"", StringUtils.wrap("ab", "\""));
2527         assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", "\""));
2528         assertEquals("'ab'", StringUtils.wrap("ab", "'"));
2529         assertEquals("''abcd''", StringUtils.wrap("'abcd'", "'"));
2530         assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", "'"));
2531         assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", "\""));
2532     }
2533 }